Skip to content

Instantly share code, notes, and snippets.

@zergiocosta
Created September 9, 2020 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zergiocosta/af66927db4219ed74760c15c543c70b0 to your computer and use it in GitHub Desktop.
Save zergiocosta/af66927db4219ed74760c15c543c70b0 to your computer and use it in GitHub Desktop.
Validar CPF
function isCpfValid(strCPF) {
strCPF = strCPF.replace(/\./g, '').replace('-', '');
var Soma;
var Resto;
Soma = 0;
if (strCPF == "00000000000") return false;
for (i=1; i<=9; i++) Soma = Soma + parseInt(strCPF.substring(i-1, i)) * (11 - i);
Resto = (Soma * 10) % 11;
if ((Resto == 10) || (Resto == 11)) Resto = 0;
if (Resto != parseInt(strCPF.substring(9, 10)) ) return false;
Soma = 0;
for (i = 1; i <= 10; i++) Soma = Soma + parseInt(strCPF.substring(i-1, i)) * (12 - i);
Resto = (Soma * 10) % 11;
if ((Resto == 10) || (Resto == 11)) Resto = 0;
if (Resto != parseInt(strCPF.substring(10, 11) ) ) return false;
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment