Skip to content

Instantly share code, notes, and snippets.

@carrerasrodrigo
Created December 28, 2013 21:14
Show Gist options
  • Save carrerasrodrigo/8164290 to your computer and use it in GitHub Desktop.
Save carrerasrodrigo/8164290 to your computer and use it in GitHub Desktop.
This functions helps to verify if a CPF (brazilian document) is valid.
function verify_cpf(cpf){
/* This functions helps to verify if a CPF (brazilian document)
it's valid. Returns true if it is, false otherwise */
cpf = cpf.replace(/\./g, "").replace(/\-/g, "");
if (cpf.length != 11){
return false;
}
var tt = 0, d =0;
for (var t=9; t>=9 && t<11; t++){
d = 0;
for (var c=0; c<t; c++){
d += parseInt(cpf.substring(c, c+1)) * (t + 1 - c);
tt = c;
}
d = ((10 * d) % 11) % 10;
if (parseInt(cpf.substring(tt+1, tt+2)) != d){
return false;
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment