Skip to content

Instantly share code, notes, and snippets.

@lucasprogamer
Created August 14, 2020 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucasprogamer/4623381e833601efe95b3777a4ac27b8 to your computer and use it in GitHub Desktop.
Save lucasprogamer/4623381e833601efe95b3777a4ac27b8 to your computer and use it in GitHub Desktop.
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