Javascript removing zero numbers at the beginning of a string

Asked

Viewed 2,172 times

-1

Gentlemen(s)

I have a function that unlinks an inscription by Cpf, but when I pass a value that starts with zero by parameter the numbers are removed. I have tried almost everything to solve the problem, when I perform searches related to this subject, basically I only find how remove zeros and do not keep.

The input I’m storing Cpf is like "text".

Code in html

 <button title="Deletar Inscrição" type="button" onclick="$('#ModalConfirmacao').modal('toggle');
                                            $('#CpfSelecionado').val(@item.Inscricao.cpf_candidato);
                                            $('#CursoSelecionado').val(@item.Inscricao.cd_curso);
                                            $('#EdicaoSelecionada').val(@item.Inscricao.cd_edicao);">
                                                    <span class="fa fa-remove"></span>
                                                </button> 

Code js

function DesvincularInscricaoNovo() {
    var cpf = document.getElementById('CpfSelecionado').value;
    var curso = document.getElementById('CursoSelecionado').value;
    var edicao = document.getElementById('EdicaoSelecionada').value;

    $.ajax({
        type: "POST",
        url: urlPortal + 'DesvincularInscricao?cpf=' + cpf +
            "&cursoId=" + curso +
            "&edicaoId=" + edicao,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            if (!data.Error) {
                $('#ModalSucesso').modal('toggle');
                $('#textModal').empty().text(data.Mensagem);
                //document.getElementById('inscricoesBody').deleteRow(document.getElementById('Cliquei').value);
            }
        }
    });
}

Parameter passed: 00885355849

Value returned in function: 885355849

  • 2

    You want an answer or you want a kick. If you want an answer put the element whose id is CpfSelecionado and all related code(events, validation, formatting, mask,...)

2 answers

2


The problem is that you are passing the direct number as the .val(). So the zeros on the left will be ignored.

Supposing @item.Inscricao.cpf_candidato be it 00885355849, then jQuery in onclick where you add the value in the input would be:

$('#CpfSelecionado').val(00885355849);

However, as you are passing a purely numerical value on .val(), the zeros on the left will be ignored and will add only 885355849 in the input.

The correct one would be that the value is a string, that is, in quotes, like this:

$('#CpfSelecionado').val('00885355849');

So to solve the problem, just put @item.Inscricao.cpf_candidato in quotes:

$('#CpfSelecionado').val('@item.Inscricao.cpf_candidato');
                         ↑                             ↑
  • Excellent! I managed to solve, thank you very much Sam!

  • Good q solved. Now just mark the answer above. ;)

1

This is because the JS ignores the zeros on the left if the type is number, try switching to string, and see if you can use it that way. If you need to use type number, you can use parseint()

Here’s a link that can help you!

https://flaviocopes.com/how-to-trim-leading-zero-number/

  • I tried to use type text and number, but I still have the same problem. Thanks for the link.

Browser other questions tagged

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