Skip to content

Instantly share code, notes, and snippets.

@diogoalexsmachado
Created July 16, 2018 14:06
Show Gist options
  • Save diogoalexsmachado/581d6aab90acb2164a072bfd215d5534 to your computer and use it in GitHub Desktop.
Save diogoalexsmachado/581d6aab90acb2164a072bfd215d5534 to your computer and use it in GitHub Desktop.
function validarNISS() {
var niss = $("#inputNISS").val();
//deve ter 11 dígitos
if(niss.length != 11) {
swal("Aviso", "O número de Segurança Social deverá ter 11 dígitos.", "warning");
} else {
var FACTORS = [29, 23, 19, 17, 13, 11, 7, 5, 3, 2];
var nissArray = [];
for (var i = 0; i < niss.length; i++) {
nissArray[i] = niss.charAt(i);
}
var sum=0;
//faz a soma do digito [j] x o dígito [j] do array dos fatores
for (var j = 0; j < FACTORS.length; j++) {
sum += nissArray[j] * FACTORS[j];
}
//verifica se dá resto 0
if (nissArray[nissArray.length - 1] == (9 - (sum % 10))) {
msgsucesso_valido()
} else {
swal("Aviso", "O número de Segurança Social não é valido.", "warning");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment