Skip to content

Instantly share code, notes, and snippets.

@Pipas
Last active January 18, 2023 19:33
Show Gist options
  • Save Pipas/69ab5b514a53a494ec2a80669e950569 to your computer and use it in GitHub Desktop.
Save Pipas/69ab5b514a53a494ec2a80669e950569 to your computer and use it in GitHub Desktop.
Validação de NIF Portugal com Javascript
function validateNIF(nif)
{
if(!['1', '2', '3', '5', '6', '8'].includes(nif.substr(0,1)) &&
!['45', '70', '71', '72', '77', '79', '90', '91', '98', '99'].includes(nif.substr(0,2)))
return false;
let total = nif[0] * 9 + nif[1] * 8 + nif[2] * 7 + nif[3] * 6 + nif[4] * 5 + nif[5] * 4 + nif[6] * 3 + nif[7] * 2;
let modulo11 = total - parseInt(total / 11) * 11;
let comparador = modulo11 == 1 || modulo11 == 0 ? 0 : 11 - modulo11;
return nif[8] == comparador
}
@raibtoffoletto
Copy link

Muito grato!

Ps: Talvez o uso de === (datatype check) seja melhor para evitar mensagens do eslint. ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment