Fórum Rotina para validar cpf e cnpj. #226979
19/04/2004
0
Sou novo em delphi e estou precisando validar cpf e cnpj.
Peguei esta rotina de validação mais não sei como usa-la.
Tipo: onde coloca-la.
Se alguem puder me ajudar ficarei grato. :) :)
ai vai a rotina.
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;
Vitoreduardo
Curtir tópico
+ 0Posts
19/04/2004
Lucas Silva
if not ChecaCpf(edit1.text) then SHowMessage(´O CPF não é válido!´)
Gostei + 0
19/04/2004
Vitoreduardo
Poderia me explicar melhor.
Gostei + 0
19/04/2004
Marco Salles
Insira no Form Dois Edits e Dois Butoes..Mude a Propriedade Name Dos Edits Respctivamente Para:EditCpf e EditCnpj Respectivamente...Mude Tambem a Propriedade Name Dos Buttões Para BtValidarCpf e BtValidarCnpj...Nos Evento OnClick do Botão BtValidarCpf Escreva:
procedure TForm1.BtValidarCpfClick(Sender: TObject); begin if ChecaCpf(EditCpf.Text) then showmessage(´Verdadeiro´) else showmessage(´Falso´) end;
Da mesma Forma Faça para o evento onclick do botão BtValidarCnpj:
procedure TForm1.BtValidarCnpjClick(Sender: TObject); begin if ChecaCnpj(EditCnpj.Text) then showmessage(´Verdadeiro´) else showmessage(´Falso´) end;
Como voce mesmo disse:
[b:7b6461bffd][size=18:7b6461bffd]então não esqueça de colocar as funcoes [/size:7b6461bffd][/b:7b6461bffd]
function ChecaCpf(CPF_Text: String): Boolean; begin ............ end; function ChecaCnpj(xCNPJ: String) : Boolean; begin ....... end;
[b:7b6461bffd]Antes dos códigos dos dois eventos onclick dos botões[/b:7b6461bffd]
deu para entender :?: :?: :?:
Gostei + 0
19/04/2004
Vitoreduardo
Valeu pela explicação.
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)