Get full number of an input

Asked

Viewed 607 times

8

In a <input>, have the following value:

00001545455

When I recover the same with Javascript, he comes without the first 4 zeros.

Can someone help me?

I am already passing it in the function I am using. The problem is that when it arrives in the function 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>
  • 3

    How are you recovering that value?

  • You have to put in the code you’re using, or I can’t guess. Take a look here -> https://jsfiddle.net/h565yvmr/ something in your code is doing parse that value

  • 3

    First, edit your question with the code. Don’t post it in the comments. Second, you are passing the number as a number in Json, not as a numeric string. Therefore, there is no way to recover these 0 left unless you know what the size of your field, and then padding 0’s left manually.

  • Here is working ok

  • Pass number as string must resolve: { 'name': 'cd_cnpj_cpf', 'value': '00001545455' } , there is no number with zeros in front, to keep the zeros, you should leave it as string.

4 answers

4

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

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

you’re using the Type numerical. That is, if you do:

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

He gives 1545455 because it works as a number. If you want to keep the zeros you have to use Type string, ie quotes 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>

1

in jQuery you can do so:

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

1

There is no recognized number, mathematically speaking, started by zeros, this is just a numerical formatting, so you must put its value in string format, with quotes, 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>

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 zeros on the left.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.