Fórum Validação de CPF #209296
25/01/2004
0
Cordialmente,
Paulo
Maikow
Curtir tópico
+ 0Posts
25/01/2004
Welington
function ChecaCpf(CPF_Text: String): Boolean;
var
n1,n2,n3,n4,n5,n6,n7,n8,n9: Integer;
d1,d2: Integer;
Digitado, Calculado: String;
begin
try
n1 := StrToInt (CPF_Text[1]);
n2 := StrToInt (CPF_Text[2]);
n3 := StrToInt (CPF_Text[3]);
n4 := StrToInt (CPF_Text[5]);
n5 := StrToInt (CPF_Text[6]);
n6 := StrToInt (CPF_Text[7]);
n7 := StrToInt (CPF_Text[9]);
n8 := StrToInt (CPF_Text[10]);
n9 := StrToInt (CPF_Text[11]);
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 := CPF_Text[13] + CPF_Text[14];
if Calculado = Digitado then
Result := True
else
Result := False;
except
Result := False;
end;
end;
function ChecaCnpj(xCNPJ: String) : Boolean;
var
d1,d4,xx,nCount,Fator,Resto,Digito1,Digito2 : Integer;
Check : String;
begin
try
d1 := 0;
d4 := 0;
xx := 1;
for nCount := 1 to Length(xCNPJ) - 2 do
begin
if Pos (Copy (xCNPJ, nCount, 1), ´/-.´) = 0 then
begin
if xx < 5 then
Fator := 6 - xx
else
Fator := 14 - xx;
d1 := d1 + StrToInt (Copy (xCNPJ, nCount, 1)) * Fator;
if xx < 6 then
Fator := 7 - xx
else
Fator := 15 - xx;
d4 := d4 + StrToInt (Copy (xCNPJ, nCount, 1)) * Fator;
xx := xx + 1;
end;
end;
Resto := (d1 mod 11);
if resto < 2 then
Digito1 := 0
else
Digito1 := 11 - Resto;
d4 := d4 + 2 * Digito1;
resto := (d4 mod 11);
if Resto < 2 then
Digito2 := 0
else
Digito2 := 11 - Resto;
Check := IntToStr(Digito1) + IntToStr(Digito2);
if Check <> Copy(xCNPJ, Succ(Length (xCNPJ) - 2), 2) then
Result := False
else
Result := True;
except
Result := False;
end;
end;
Gostei + 0
25/01/2004
Aroldo Zanela
Outra rotina de validação de CPF:
Function ValidCPF(const s:string): Boolean; var i, Numero, Resto : Byte ; DV1, DV2 : Byte ; Total, Soma : Integer ; Begin result := FALSE; if length (AllTrim (s)) = 11 Then begin Total := 0 ; Soma := 0 ; for i := 1 to 9 do Begin Try Numero := StrToInt (s[i]); Except Numero := 0; end; Total := Total + (Numero * (11 - i)) ; Soma := Soma + Numero; end; Resto := Total mod 11; if Resto > 1 then DV1 := 11 - Resto else DV1 := 0; Total := Total + Soma + 2 * DV1; Resto := Total mod 11; if Resto > 1 then DV2 := 11 - Resto else DV2 := 0; if (IntToStr (DV1) = s[10]) and (IntToStr (DV2) = s[11]) then result := TRUE; end; end;
Gostei + 0
26/01/2004
Maikow
Fico muito grato pelas rotinas enviadas. Com certeza, serão de grande valia para mim!
Espero poder sempre participar deste fórum, interagindo mutuamente com os colegas, sendo ajudado e ajudando também, com o intuito de que este seja, cada vez mais, um espaço autêntico e democrático para a divulgação e estudo coletivo do Delphi.
Cordiais abraços,
Paulo :)
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)