Last active
March 21, 2021 20:41
-
-
Save webarthur/abaa99b20d4ff53b9198d380a520ff60 to your computer and use it in GitHub Desktop.
Função JavaScript para validar CPF
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 validaCPF(cpf) { | |
var Soma = 0 | |
var Resto | |
var strCPF = String(cpf).replace(/[^\d]/g, '') | |
if (strCPF.length !== 11) | |
return false | |
if ([ | |
'00000000000', | |
'11111111111', | |
'22222222222', | |
'33333333333', | |
'44444444444', | |
'55555555555', | |
'66666666666', | |
'77777777777', | |
'88888888888', | |
'99999999999', | |
].indexOf(strCPF) !== -1) | |
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