Validar CPF em PHP (Completo)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function validaCPF($cpf) { | |
// Extrai somente os números | |
$cpf = preg_replace( '/[^0-9]/is', '', $cpf ); | |
// Verifica se foi informado todos os digitos corretamente | |
if (strlen($cpf) != 11) { | |
return false; | |
} | |
// Verifica se foi informada uma sequência de digitos repetidos. Ex: 111.111.111-11 | |
if (preg_match('/(\d)\1{10}/', $cpf)) { | |
return false; | |
} | |
// Faz o calculo para validar o CPF | |
for ($t = 9; $t < 11; $t++) { | |
for ($d = 0, $c = 0; $c < $t; $c++) { | |
$d += $cpf[$c] * (($t + 1) - $c); | |
} | |
$d = ((10 * $d) % 11) % 10; | |
if ($cpf[$c] != $d) { | |
return false; | |
} | |
} | |
return true; | |
} |
@_rafaelneri valeu meu jovem, sucesso e forte abraço!
Ele verifica a existência ou não do cpf?
Isso não é possivel fazer.
Eu costumo validar o cpf (em javascript) assim, valida super bem, até mesmo se existe ou não
/*
* return true|false
*/
ValidaCPF(cpf){
const strCPF = cpf.replaceAll(/\D/g , "");
var Soma = 0;
for (var i=1; i<=9; i++) {
var Soma = Soma + parseInt(strCPF.substring(i - 1, i)) * (11 - i);
}
var Resto = (Soma * 10) % 11;
if ((Resto == 10) || (Resto == 11)) Resto = 0;
if (Resto != parseInt(strCPF.substring(9, 10)) ) {
return false;
}
Soma = 0;
for (i = 1; i <= 10; i++) {
Soma = Soma + parseInt(strCPF.substring(i - 1, i)) * (12 - i)
}
Resto = (Soma * 10) % 11;
if ((Resto == 10) || (Resto == 11)) Resto = 0;
if (Resto != parseInt(strCPF.substring(10, 11) ) ) {
return false;
}
return true;
}
Eu costumo validar o cpf (em javascript) assim, valida super bem, até mesmo se existe ou não
/* * return true|false */ ValidaCPF(cpf){ const strCPF = cpf.replaceAll(/\D/g , ""); var Soma = 0; for (var i=1; i<=9; i++) { var Soma = Soma + parseInt(strCPF.substring(i - 1, i)) * (11 - i); } var Resto = (Soma * 10) % 11; if ((Resto == 10) || (Resto == 11)) Resto = 0; if (Resto != parseInt(strCPF.substring(9, 10)) ) { return false; } Soma = 0; for (i = 1; i <= 10; i++) { Soma = Soma + parseInt(strCPF.substring(i - 1, i)) * (12 - i) } Resto = (Soma * 10) % 11; if ((Resto == 10) || (Resto == 11)) Resto = 0; if (Resto != parseInt(strCPF.substring(10, 11) ) ) { return false; } return true; }
Seu código não valida se existe um CPF, ele supostamente só valida se a sequência de números é uma sequência válida, isso não significa que o CPF exista de fato. São coisas diferentes. Unica maneira de se validar a existência de um CPF é através do site da Receita Federal.
Show de bola, parabéns!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@indianabytes eu fui verificar e realmente ele mostra essa função no ChatGPT.
@juniormartinxo no caso do Copilot ele aprende com os códigos do Github.
Isso significa que tem muito código que ainda usa o formato antigo, sem as sugestões do @jonasbraga.