Get complete number of an input

8

In <input> , I have the following value:

  

00001545455

When I retrieve the same with Javascript , it comes without the first 4 zeros.

Can anyone help me?

I'm already passing it on to the function I'm using. The problem is that when it arrives in the function it comes without the zeros:

<a class="btnGrid bt_consulta btsAcoes" titulo="Consultar" href="javascript:;" onclick="GVScheduleCommand('gvClientes',['Consultar',3],1,[{ 'name': 'cd_cnpj_cpf', 'value': 00001545455 }], 'CPF_CNPJ' );"></a>
    
asked by anonymous 12.03.2015 / 17:18

4 answers

4

When you define an object like the one you mentioned in the question:

{ 'name': 'cd_cnpj_cpf', 'value': 00001545455 }

You are using Type numeric. That is if you do:

var dados = { 'name': 'cd_cnpj_cpf', 'value': 00001545455 };
typeof dados.value // vai dar "number"
dados.value // vai dar 00001545455

It gives 1545455 because it interprets as number. If you want to keep the zeros you have to use Type string, ie quotation marks around these numbers. Example:

{ 'name': 'cd_cnpj_cpf', 'value': '00001545455' }

and therefore:

var dados = { 'name': 'cd_cnpj_cpf', 'value': '00001545455' };
typeof dados.value // vai dar "string"
dados.value // vai dar '00001545455'

In your code it would be:

<a class="btnGrid bt_consulta btsAcoes" titulo="Consultar" href="javascript:;" onclick="GVScheduleCommand('gvClientes',['Consultar',3],1,[{ 'name': 'cd_cnpj_cpf', 'value': '00001545455' }], 'CPF_CNPJ' );"></a>
    
24.11.2015 / 18:20
1

In jQuery you can do this:

var valor = $('input').val();
$('div').append(valor);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script><inputtype="text" value="00001545455 " />
<div id="resultado"></div>
    
25.06.2015 / 20:12
1

There is no recognized number, mathematically speaking, starting with zeros, this is just numeric formatting, so you should put your value in string format, with quotation marks, so that it is kept in the expected format:

<a class="btnGrid bt_consulta btsAcoes" titulo="Consultar" href="javascript:void(0);" onclick="GVScheduleCommand('gvClientes',['Consultar',3],1,[{ 'name': 'cd_cnpj_cpf', 'value': '00001545455' }], 'CPF_CNPJ' );"></a>
    
24.11.2015 / 21:44
0

To get the value of the element do

var valorComZeros = document.getElementById('idDoSeuInput').value;

You are probably doing a ParseInt () and this will remove the leading zeros.

    
16.03.2015 / 13:23