﻿// Copyright - 2000-2005 Alfredo J G A Borba. Todos os Direitos Reservados

function cpfValidator(value) {

 var soma1, soma2, s1, s2;
 var cpf = value;
 var str_cpf = "";
 var verifzero = 0; 

 for (i = 0; i <= cpf.length - 1; i++)   
   if ((cpf.charAt(i)).match(/\d/)) {
     str_cpf += cpf.charAt(i);
     verifzero += cpf.charAt(i);
   }
   else if (!(cpf.charAt(i)).match(/[\.\-]/)) 
     return false;

 if (str_cpf.length != 11)
   return false;

 if (verifzero == 0) 
   return false;

 if (str_cpf == 11111111111 || str_cpf == 22222222222 || str_cpf == 33333333333 || str_cpf == 44444444444 || str_cpf == 55555555555 || str_cpf == 66666666666 || str_cpf == 77777777777 || str_cpf == 88888888888 || str_cpf == 99999999999)
   return false; 

 soma1 = soma2 = 0;

 for (i = 0; i <= 8; i++) {
   soma1 += str_cpf.charAt(i) * (10-i);
   soma2 += str_cpf.charAt(i) * (11-i);
 }

 s1 = ((soma1 * 10) % 11) % 10;
 s2 = (((soma2 + (s1 * 2)) * 10) % 11) % 10;

 if ((s1 != str_cpf.charAt(9)) || (s2 != str_cpf.charAt(10)))
    return false;

 return true;
}
