mai 25
Com esta função você pode validar CPFs, há duas versões, uma para PHP e outra em JS. Bom proveito.
Versão PHP:
-
function validaCPF($cpf) {
-
$cpf = soNumero($cpf);
-
$soma = 0;
-
-
for ($i = 0; $i < 9; $i++) {
-
$soma += (10 – $i) * $cpf[$i];
-
}
-
$digitoVerificador = 11 – ($soma % 11);
-
-
if(($soma % 11) < 2) {
-
$digitoVerificador = 0;
-
}
-
if($cpf[9] != $digitoVerificador) {
-
return false;
-
}
-
$soma = 0;
-
-
for ($i = 0; $i < 9; $i++) {
-
$soma += (11 – $i) * $cpf[$i];
-
}
-
$soma += 2 * $cpf[9];
-
$digitoVerificador = 11 – ($soma % 11);
-
-
if(($soma % 11) < 2) {
-
$digitoVerificador = 0;
-
}
-
if($cpf[10] != $digitoVerificador) {
-
return false;
-
}
-
return true;
-
}
Versão Javascript:
-
function validaCPF(cpf) {
-
cpf = soNumero(cpf);
-
soma = 0;
-
-
for (i = 0; i < 9; i++) {
-
soma += (10 – i) * (eval(cpf.charAt(i)));
-
}
-
digitoVerificador = 11 – (soma % 11);
-
-
if((soma % 11) < 2) {
-
digitoVerificador = 0;
-
}
-
if(eval(cpf.charAt(9)) != digitoVerificador) {
-
return false;
-
}
-
soma = 0;
-
-
for (i = 0; i < 9; i++) {
-
soma += (11 – i) * (eval(cpf.charAt(i)));
-
}
-
soma += 2 * (eval(cpf.charAt(9)));
-
digitoVerificador = 11 – (soma % 11);
-
-
if((soma % 11) < 2) {
-
digitoVerificador = 0;
-
}
-
if(eval(cpf.charAt(10)) != digito_verificador) {
-
return false;
-
}
-
return true;
-
}

Recent Comments