Skip to content

Instantly share code, notes, and snippets.

@YurePereira
Last active June 19, 2019 14:52
Show Gist options
  • Select an option

  • Save YurePereira/afb504919974a3c0dac1a9ce04e426d6 to your computer and use it in GitHub Desktop.

Select an option

Save YurePereira/afb504919974a3c0dac1a9ce04e426d6 to your computer and use it in GitHub Desktop.
/**
*
* Função que faz a validação de CPF, verificando se ele está no formato certo, exemplo 'xxx.xxx.xxx-xx' ou 'xxxxxxxxxxx', e se
* ele é válido, caso as duas verificação sejam verdadeiras será retornado TRUE ou FALSE caso contrario.
*
* @param c: String CNPJ a se validado.
* @return Boolean
*
*/
function validCPF(c) {
if (c.match(/^[0-9]{3}\.?[0-9]{3}\.?[0-9]{3}\-?[0-9]{2}$/)) {
c = c.replace(/(\.|\-)/g,"");
var s = 0,
i = 0,
i2 = 0,
j = 0,
cpf = [];
for (i = 0; i <= 8; i++) {
cpf[i] = c.charAt(i);
}
for (i = 10; i >= 2; i--) {
s += cpf[j] * i;
j += 1;
}
cpf[9] = (s % 11 > 2) ? 11 - (s % 11) : 0;
s = 0;
j = 0;
for (i = 11; i >= 2; i--) {
s += cpf[j] * i;
j += 1;
}
cpf[10] = (s % 11 > 2) ? 11 - (s % 11) : 0;
s = 0;
j = 0;
return cpf[9] == c.charAt(c.length - 2) && cpf[10] == c.charAt(c.length - 1);
} else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment