Fórum Nao consigo validar CPF e CNPJ mascarado! #198448
02/12/2003
0
Já li varios topicos aqui nesse forum mas nao consegui aplicar no meu programa.
Sou novato em programação, se alguem puder me ajudar ficarei grato.
No msn meu nick é thallesrodrigo@hotmail.com
Se puderem me ajudar ficarei grato!
Imaster
Curtir tópico
+ 0Posts
02/12/2003
Lucas Silva
qual é a máscara que você colocou pro cnpj e pro cpf?
Gostei + 0
02/12/2003
Imaster
000\.000\.000\-00;0;_
Gostei + 0
02/12/2003
Lucas Silva
Gostei + 0
02/12/2003
Dinorvm
chama-se MDLib e pode ser encontrado no endereço:
http://codigolivre.org.br/projects/mdlib
Se tiver dificuldades, pode me contatar:
dinorvm@ig.com.br
Gostei + 0
02/12/2003
Rafael Mattos
function CPF(num: string): boolean;
var
n1,n2,n3,n4,n5,n6,n7,n8,n9: integer;
d1,d2: integer;
digitado, calculado: string;
begin
// 000.000.001-91
n1:=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]);
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[13]+num[14];
if calculado=digitado then
cpf:=true
else
cpf:=false;
end;
function CNPJ(num: string): boolean;
var
n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12: integer;
d1,d2: integer;
digitado, calculado: string;
begin
// 00.000.000/0001-91
n1:=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]);
d1:=n12*2+n11*3+n10*4+n9*5+n8*6+n7*7+n6*8+n5*9+n4*2+n3*3+n2*4+n1*5;
d1:=11-(d1 mod 11);
if d1>=10 then
d1:=0;
d2:=d1*2+n12*3+n11*4+n10*5+n9*6+n8*7+n7*8+n6*9+n5*2+n4*3+n3*4+n2*5+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 then
CNPJ:=true
else
CNPJ:=false;
end;
Procedure ToValidate(NomeEdit : TDBEdit; Valid : String); // CPF ou CNPJ
begin
if NomeEdit.DataSource.State <> dsBrowse then begin
if Valid = ´CNPJ´ then begin
if NomeEdit.Text = ´ . . / - ´ then
Exit; end
else if Valid = ´CPF´ then begin
if NomeEdit.Text = ´ . . - ´ then
Exit;
end;
if Valid = ´CNPJ´ then begin
if not CNPJ(NomeEdit.Text) then begin
beep;
MessageDlg(´CNPJ Incorreto´,mtInformation,[mbOK],0);
NomeEdit.SetFocus;
Exit;
end; end
else if Valid = ´CPF´ then begin
if not CPF(NomeEdit.Text) then begin
beep;
MessageDlg(´CPF Incorreto´,mtInformation,[mbOK],0);
NomeEdit.SetFocus;
Exit;
end;
end;
end;
end;
No OnExit do DBEdit vc coloca assim:
procedure TfrmPrincipal.DBEdit1Exit(Sender: TObject);
begin
ToValidate(NomeEdit,´CPF´); // ou CNPJ se deseja verificar
end;
Gostei + 0
02/12/2003
Raul Segabinazzi
copy(cpf,1,3)+´.´+copy(cpf,4,3)+´.´+copy(cpf,7,3)+´-´+copy(cpf,10,2);
Gostei + 0
03/12/2003
Imaster
Valeu as dicas
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)