Last active
September 13, 2022 20:42
-
-
Save robertopc/ec1f8459662869d728c9e7253ec61102 to your computer and use it in GitHub Desktop.
Validação de CPF Javascript
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
//valida o CPF digitado | |
function validarCPF(cpf){ | |
cpf = cpf.toString().replace(/\.|\-/g, '' ); | |
if(cpf.match(/^(1+|2+|3+|4+|5+|6+|7+|8+|9+|0+)$/) || cpf.length != 11) | |
return false; | |
soma = 0; | |
for(i = 0; i < 9; i ++) { | |
soma += parseInt(cpf.charAt(i)) * (10 - i); | |
} | |
resto = 11 - (soma % 11); | |
if(resto == 10 || resto == 11) { | |
resto = 0; | |
} | |
if(resto != parseInt(cpf.charAt(9))) { | |
return false; | |
} | |
soma = 0; | |
for (i = 0; i < 10; i ++) { | |
soma += parseInt(cpf.charAt(i)) * (11 - i); | |
} | |
resto = 11 - (soma % 11); | |
if(resto == 10 || resto == 11) { | |
resto = 0; | |
} | |
if(resto != parseInt(cpf.charAt(10))) { | |
return false; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment