Created
August 14, 2020 17:08
-
-
Save lucasprogamer/4623381e833601efe95b3777a4ac27b8 to your computer and use it in GitHub Desktop.
validador de cpf
This file contains hidden or 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
cfpValido () { | |
const regexCPF = /^\d{3}\.\d{3}\.\d{3}-\d{2}$/ | |
let i = 0 | |
let soma = 0 | |
let resto = 0 | |
if (regexCPF.test(this.cpf)) { | |
for (i = 1; i <= 9; i++) { | |
soma = soma + parseInt(this.cpf.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(this.cpf.substring(9, 10))) { | |
return false | |
} | |
soma = 0 | |
for (i = 1; i <= 10; i++) { | |
soma = soma + parseInt(this.cpf.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(this.cpf.substring(10, 11))) { | |
return false | |
} | |
return true | |
} | |
return false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment