Skip to content

Instantly share code, notes, and snippets.

@rafacv
Created September 23, 2009 12:17
Show Gist options
  • Select an option

  • Save rafacv/191941 to your computer and use it in GitHub Desktop.

Select an option

Save rafacv/191941 to your computer and use it in GitHub Desktop.
Simple JS function to validate CPF numbers
// a simple js function to validate CPF numbers
function iscpf(c){
if(!isNaN(c))
c = ""+c;
var re = /^(\d{1})(\d{1})(\d{1})(\d{1})(\d{1})(\d{1})(\d{1})(\d{1})\d{3}$/.exec(c);
if(!re)
return false;
var i = 2, j = 0, k = 0, g = re[1];
for(;i<re.length;i++)
if(re[i]!=g)
break;
if(i==9)
return false;
c = c.split("");
for(i=0;i<9;i++)
j+=c[i]*(10-i);
for(i=0;i<9;i++)
k+=c[i]*(11-i);
j = 11-(j%11);
j = (j<10)?j : 0;
k += j*2;
k = 11-(k%11);
k = (k<10)?k : 0;
return (j==c[9] && k==c[10]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment