Created
November 24, 2015 18:50
-
-
Save felipekm/494f2a981ad83d9bcd96 to your computer and use it in GitHub Desktop.
validaCpf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function VerificaCPF(strCpf) { | |
if (!strCpf || strCpf === '') { | |
return false; | |
} | |
var soma = 0, | |
resto; | |
for (i = 1; i <= 9; i += 1) { | |
soma = soma + parseInt(strCpf.substring(i - 1, i)) * (11 - i); | |
} | |
resto = soma % 11; | |
if (resto == 10 || resto == 11 || resto < 2) { | |
resto = 0; | |
} else { | |
resto = 11 - resto; | |
} | |
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 % 11; | |
if (resto == 10 || resto == 11 || resto < 2) { | |
resto = 0; | |
} else { | |
resto = 11 - resto; | |
} | |
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