Nao consigo validar CPF e CNPJ mascarado!
Galera, faz dois dias que estou lendo, tentando de tudo o que é maneira e nao consigo validar cpf e cnpj com mascara.
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!
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
Curtidas 0
Respostas
Lucas Silva
02/12/2003
Bom cara eu não msn naõ, se você detalhar mais ai da pra te ajudar.
qual é a máscara que você colocou pro cnpj e pro cpf?
qual é a máscara que você colocou pro cnpj e pro cpf?
GOSTEI 0
Imaster
02/12/2003
A mascara para CPF é essa:
000\.000\.000\-00;0;_
000\.000\.000\-00;0;_
GOSTEI 0
Lucas Silva
02/12/2003
Este este método seu de verificar cpf e cnpj, verifica blz quando o cpf ou cnpj está sem máscara??
GOSTEI 0
Dinorvm
02/12/2003
Amigo, já sofri com isso tb, mas, encontrei um pacote de componentes que entre outras coisas, traz um verificador automatico e inclusive com formatação...
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
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
Rafael Mattos
02/12/2003
Eu ja tive o mesmo problema que vc dai eu resolvi alterar a função que peguei do clubedelphi.
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;
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
Raul Segabinazzi
02/12/2003
O mais simples seria você fazer, no evendo on exit, primeiro a validação do CPF ou CGC para depois fazer a formatação usando copy. ex.
copy(cpf,1,3)+´.´+copy(cpf,4,3)+´.´+copy(cpf,7,3)+´-´+copy(cpf,10,2);
copy(cpf,1,3)+´.´+copy(cpf,4,3)+´.´+copy(cpf,7,3)+´-´+copy(cpf,10,2);
GOSTEI 0
Imaster
02/12/2003
Consegui
Valeu as dicas
Valeu as dicas
GOSTEI 0