Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
validador de cpf
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
You can’t perform that action at this time.