Skip to content

Instantly share code, notes, and snippets.

@klebergraciasoares
Created June 28, 2013 18:01
Show Gist options
  • Save klebergraciasoares/5886701 to your computer and use it in GitHub Desktop.
Save klebergraciasoares/5886701 to your computer and use it in GitHub Desktop.
jQuery.validarCPF = function(cpf) {
cpf = cpf.replace(/[^\d]+/g,'');
if(cpf == '' || cpf.length != 11)
return false;
// Valida primeiro digito
add = 0;
for (i=0; i < 9; i ++)
add += parseInt(cpf.charAt(i)) * (10 - i);
rev = 11 - (add % 11);
if (rev == 10 || rev == 11)
rev = 0;
if (rev != parseInt(cpf.charAt(9)))
return false;
// Valida segundo digito
add = 0;
for (i = 0; i < 10; i ++)
add += parseInt(cpf.charAt(i)) * (11 - i);
rev = 11 - (add % 11);
if (rev == 10 || rev == 11)
rev = 0;
if (rev != parseInt(cpf.charAt(10)))
return false;
return true;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment