How do I differentiate between foreign and national Fps in javascript?

Asked

Viewed 118 times

-1

I’m making a system that needs to validate the Brazilian field Cpf but if Cpf is foreign does not need to validate the validation part of Cpf is OK, but how would I put in the function to disregard foreign Cpf.

follows the code:

//valida o CPF
function verificarCPF(c){
    var i;
    s = c;
    var c = s.substr(0,9);
    var dv = s.substr(9,2);
    var d1 = 0;
    var v = false;

    for (i = 0; i < 9; i++){
        d1 += c.charAt(i)*(10-i);
    }
    if (d1 == 0){
        alert("CPF Inválido")
        v = true;
        return false;
    }
    d1 = 11 - (d1 % 11);
    if (d1 > 9) d1 = 0;
    if (dv.charAt(0) != d1){
        alert("CPF Inválido")
        v = true;
        return false;
    }

    d1 *= 2;
    for (i = 0; i < 9; i++){
        d1 += c.charAt(i)*(11-i);
    }
    d1 = 11 - (d1 % 11);
    if (d1 > 9) d1 = 0;
    if (dv.charAt(1) != d1){
        alert("CPF Inválido")
        v = true;
        return false;
    }
    if (!v) {
        //alert(c + "nCPF Válido")
    }
}
  • What is a foreign number?

  • CPF is a Brazilian document. Therefore, I believe that all CPF should be validated if inserted. Of course, if the foreign CPF you are referring to is that CPF that is made for foreign people who come to Brazil.

  • Foreign CPF is different?

  • 1

    The question was half vacant for example you want to know a foreigner who has CPF (Brazilian document), Or a foreign document like the social service number in the USA that would be the equivalent of our CPF If it is the CPF there is as the last digit before the check digit is referring to the tax region of Brazil where the CPF was issued

  • Foreign CPF is equal to Brazilian CPF. It has the same standard of numbers.

1 answer

4

The CPF of Brazilians and foreigners has the same number of digits, so the validation will always be the same and there is no way to differentiate them based only on the numbering.

I believe that there is no API for you to consult this information, however, what you can do is create a select on your page indicating the nationality of the CPF, and if foreign does not occur the validation.

Example:

<label>Nacionalidade CPF</label>
<select id="nacionalidadeCPF">
  <option value="1">Nacional</option>
  <option value="2">Estrangeiro</option>
</select>

And in your faith:

function verificarCPF(c){
     var nacionalidade = document.getElementById('nacionalidadeCPF').value;
     if (nacionalidade == 2) return true;

     ...
     ...
}

Browser other questions tagged

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