// VALIDAÇÃO DE CPF
procedure TDM.IBTClientesCPFSetText(Sender: TField; const Text:
String);
var i, soma, D1, D2: integer;
CPF: string;
begin
if text = '' then
sender.AsString := ''
else
if length(sender.AsString) <> 11 then
begin
showmessage('CPF Inválido');
abort;
end
else
begin
CPF := copy(Sender.AsString, 1, 9);
soma := 0;
for i := 1 to 9 do
soma := soma + (strtoint(copy(cpf, i, 1)) * (11 - i));
D1 := 11 - (soma mod 11);
if D1 >= 10 then
D1 := 0;
CPF := CPF + inttostr(D1);
soma := 0;
for i := 1 to 10 do
soma := soma + (strtoint(copy(cpf, i, 1)) * (12 - i));
D2 := 11 - (soma mod 11);
if D2 > 10 then
D2 := 0;
CPF := CPF + inttostr(D2);
if CPF <> Text then
begin
showmessage('CPF Inválido');
abort;
end
else
sender.AsString := text;
end;
end;
MAS COMO DISSE ACIMA ATÉ O MEU CPF DA DANDO INVÁLIDO
O teu CPF está correto, eu fiz um programa para calcular o dígito, é
free, você pode baixar ele em:
http://geocities.yahoo.com.br/simoesluciano/downloads/cpf.zip .
Bom a rotina de cáculo sua pode estar errada. Não estou com o
código meu aqui, amanhã posto o meu código.
Até mais.
Declare em interface, private ou public o cabeçalho da função --->
function cpf(num: string): boolean;
Passos: segure Ctrl+Shift+C, o delphi irá criar o procedimento abaixo:
function TForm1.cpf(num: string): boolean;
begin
end;
Ai basta implementar o codigo seguinte:
function TForm1.cpf(num: string): boolean;
var
n1,n2,n3,n4,n5,n6,n7,n8,n9: integer;
d1,d2: integer;
digitado, calculado: string;
begin
n1:=StrToInt(num[1]);
n2:=StrToInt(num[2]);
n3:=StrToInt(num[3]);
n4:=StrToInt(num[4]);
n5:=StrToInt(num[5]);
n6:=StrToInt(num[6]);
n7:=StrToInt(num[7]);
n8:=StrToInt(num[8]);
n9:=StrToInt(num[9]);
d1:=n9*2+n8*3+n7*4+n6*5+n5*6+n4*7+n3*8+n2*9+n1*10;
d1:=11-(d1 mod 11);
if d1>=10 then d1:=0;
d2:=d1*2+n9*3+n8*4+n7*5+n6*6+n5*7+n4*8+n3*9+n2*10+n1*11;
d2:=11-(d2 mod 11);
if d2>=10 then d2:=0;
calculado:=inttostr(d1)+inttostr(d2);
digitado:=num[10]+num[11];
if calculado=digitado then
cpf:=true
else
cpf:=false;
end;
Usando a function Ex.:
jha deu para reparar que a function retorna True ou False, Caso seja
verdadeiro o CPF ou não.
if cpf(edti1.text) = True then
ShowMessage('CPF Válido')
else
ShowMessage('CPF inválido');