[RESOLVIDO]Problemas validação CPF e CNPJ
To quase lá agora, mas ainda da um erro
vou colocar todo o codigo e ir comentando
To usando 3 funcoes
Uma para formatar o campo
function Tfboleto.FormatarCPFCNPJ(const Doc: String): String;begin if (Length(Doc) = 11) then begin Result:= Copy(Doc,1,3) + '.' + Copy(Doc,4,3) + '.' + Copy(Doc,7,3) + '-' + Copy(Doc,10,2); end; if (Length(Doc) = 14) then begin Result:= Copy(Doc,1,2) + '.' + Copy(Doc,3,3) + '.' + Copy(Doc,6,3) + '/' + Copy(Doc,9,4) + '-' + Copy(Doc,13,2); end;end;uma para validar cpf
function Tfboleto.cpf(num:string):boolean;varn1,n2,n3,n4,n5,n6,n7,n8,n9:integer;d1,d2:integer;digitado,calculado:string;beginn1:=StrToInt(num[1]);n2:=StrToInt(num[2]);n3:=StrToInt(num[3]);n4:=StrToInt(num[5]);n5:=StrToInt(num[6]);n6:=StrToInt(num[7]);n7:=StrToInt(num[9]);n8:=StrToInt(num[10]);n9:=StrToInt(num[11]);if (n1=n2) and (n2=n3) and (n3=n4) and (n4=n5) and (n5=n6)and (n6=n7) and (n7=n8) and (n8=n9) thencpf:=falseElseBegind1:=n92+n83+n74+n65+n56+n47+n38+n29+n110;d1:=11-(d1 mod 11);if d1>=10 then d1:=0;d2:=d12+n93+n84+n75+n66+n57+n48+n39+n210+n1*11;d2:=11-(d2 mod 11);if d2>=10 then d2:=0;calculado:=IntToStr(d1)+IntToStr(d2);digitado:=num[13]+num[14];if calculado=digitado thencpf:=trueelsecpf:=false;endend;uma para formatar cnpj
function Tfboleto.cnpj(num:string):Boolean;varn1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12:integer;d1,d2:integer;digitado,calculado:string;beginn1:=StrToInt(num[1]);n2:=StrToInt(num[2]);n3:=StrToInt(num[4]);n4:=StrToInt(num[5]);n5:=StrToInt(num[6]);n6:=StrToInt(num[8]);n7:=StrToInt(num[9]);n8:=StrToInt(num[10]);n9:=StrToInt(num[12]);n10:=StrToInt(num[13]);n11:=StrToInt(num[14]);n12:=StrToInt(num[15]);if (n1=n2) and (n2=n3) and (n3=n4) and (n4=n5) and (n5=n6)and (n6=n7) and (n7=n8) and (n8=n9) and (n9=n10) and (n10=n11) and (n11=n12) thencnpj:=falseelsebegind1:=n122+n113+n104+n95+n86+n77+n68+n59+n42+n33+n24+n15;d1:=11-(d1 mod 11);if d1>=10 then d1:=0;d2:=d12+n123+n114+n105+n96+n87+n78+n69+n52+n43+n34+n25+n1*6;d2:= 11-(d2 mod 11);if d2>=10 then d2:=0;calculado:=IntToStr(d1)+IntToStr(d2);digitado:=num[17]+num[18];if calculado=digitado thencnpj:=Trueelsecnpj:=false;endend;Agora uso esse codigo no evento de onexit do edit
procedure Tfboleto.EditCgccpfExit(Sender: TObject);var num_cpf, num_cnpj:string;beginEditCgccpf.Text:= FormatarCPFCNPJ(EditCgccpf.Text); //formata o campoif (Length(EditCgccpf.Text) = 11) then // valida cpfnum_cpf:=EditCgccpf.Text;if NOT cpf(EditCgccpf.Text) thenBeginShowMessage('CPF INVALIDO!!!');EditCgccpf.SetFocus;exit;end;if (Length(EditCgccpf.Text) = 14) then // valida cnpjnum_cnpj:=EditCgccpf.Text; If not cnpj(EditCgccpf.Text) thenBeginShowMessage('CNPJ INVALIDO!!!');EditCgccpf.SetFocus;exit;end;end;Agora o problema
Se uso só uma das funcoes no exit do edit funciona certo, mas usando o codigo acima com as duas da o erro abaixo qndo o valor é valido
/applications/core/interface/imageproxy/imageproxy.php?img=http://i6.tinypic.com/4ucpdu1.jpg&key=1ca5e362c47a2e3820a6c3cb3177aebdec78dc847baefd256cbe3a8b2a852e3a" alt="Imagem Postada" />
Alguma idéia?
Discussão (7)
Carregando comentários...