Validação de CPF e CNPJ... ajudem-me.

Delphi

08/06/2003

Olá pessoal,

Estou tentando validar CPF e CNPJ, mas não está dando certo (talvez por causa das máscaras que coloquei no atributo da tabela no DataModule).
Alguém pode me passar os procedimentos, para mim comparar com os meus.
Outra coisinha: Qual o melhor evento para colocar a programação de validação? Eu estou fazendo no OnExit do campo.

Obrigadoooo...


Vinciguerra

Vinciguerra

Curtidas 0

Respostas

Phibis

Phibis

08/06/2003

Caro guerra não entendi direito a situação da mascara, porem estou disponibilizando uma sugestão:

-Tire a mascara do componente.

procedure TFrmClientes.EdtCnpjExit(Sender: TObject); {evento on exit do objeto maskedit}
begin
if EdtCnpj.Text <> ´´ then
begin
If length(EdtCnpj.Text) = 11 then
cpf
else if length(EdtCnpj.Text) = 14 then
cnpj
else if (length(EdtCnpj.Text) <> 14) and (length(EdtCnpj.Text) <> 11) then
begin
ShowMessage(´Número de caracteres inválidos...´);
EdtCnpj.SetFocus;
exit;
end;
end;
end;

procedure TFrmClientes.Cnpj; {procedure que valida Cnpj}
var tot, dig1, dig2, rest1, rest2 : integer;
begin
tot := StrToInt(copy(EdtCnpj.Text,12,1))* 2 +
StrToInt(copy(EdtCnpj.Text,11,1))* 3 +
StrToInt(copy(EdtCnpj.Text,10,1))* 4 +
StrToInt(copy(EdtCnpj.Text,9,1))* 5 +
StrToInt(copy(EdtCnpj.Text,8,1))* 6 +
StrToInt(copy(EdtCnpj.Text,7,1))* 7 +
StrToInt(copy(EdtCnpj.Text,6,1))* 8 +
StrToInt(copy(EdtCnpj.Text,5,1))* 9 +
StrToInt(copy(EdtCnpj.Text,4,1))* 2 +
StrToInt(copy(EdtCnpj.Text,3,1))* 3 +
StrToInt(copy(EdtCnpj.Text,2,1))* 4 +
StrToInt(copy(EdtCnpj.Text,1,1))* 5;
rest1 := (tot mod 11);
if rest1 < 2 then
dig1 := 0
else
dig1 := 11 - rest1;
if StrToInt(copy(EdtCnpj.Text,13,1))<> dig1 then
begin
ShowMessage(´CNPJ inválido´);
EdtCnpj.SetFocus;
exit;
end;
tot := StrToInt(copy(EdtCnpj.Text,13,1))* 2 +
StrToInt(copy(EdtCnpj.Text,12,1))* 3 +
StrToInt(copy(EdtCnpj.Text,11,1))* 4 +
StrToInt(copy(EdtCnpj.Text,10,1))* 5 +
StrToInt(copy(EdtCnpj.Text,9,1))* 6 +
StrToInt(copy(EdtCnpj.Text,8,1))* 7 +
StrToInt(copy(EdtCnpj.Text,7,1))* 8 +
StrToInt(copy(EdtCnpj.Text,6,1))* 9 +
StrToInt(copy(EdtCnpj.Text,5,1))* 2 +
StrToInt(copy(EdtCnpj.Text,4,1))* 3 +
StrToInt(copy(EdtCnpj.Text,3,1))* 4 +
StrToInt(copy(EdtCnpj.Text,2,1))* 5 +
StrToInt(copy(EdtCnpj.Text,1,1))* 6;
rest2 := (tot mod 11);
if rest2 < 2 then
dig2 := 0
else
dig2 := 11 - rest2;
if StrToInt(copy(EdtCnpj.Text,14,1))<> dig2 then
begin
ShowMessage(´CNPJ inválido´);
EdtCnpj.SetFocus;
exit;
end;
if not EdtCnpj.IsMasked then
EdtCnpj.EditMask := ´99.999.999/9999-99;1;_´
end;

procedure TFrmClientes.Cpf; {procedure para validação do Cpf}
var tot, rest1, rest2, dig1, dig2 : integer;
begin
if EdtCnpj.Text <> ´´ then
begin
tot := StrToInt(copy(EdtCnpj.Text,1,1))* 10 +
StrToInt(copy(EdtCnpj.Text,2,1))* 9 +
StrToInt(copy(EdtCnpj.Text,3,1))* 8 +
StrToInt(copy(EdtCnpj.Text,4,1))* 7 +
StrToInt(copy(EdtCnpj.Text,5,1))* 6 +
StrToInt(copy(EdtCnpj.Text,6,1))* 5 +
StrToInt(copy(EdtCnpj.Text,7,1))* 4 +
StrToInt(copy(EdtCnpj.Text,8,1))* 3 +
StrToInt(copy(EdtCnpj.Text,9,1))* 2 ;

rest1 := (tot mod 11);
if rest1 < 2 then
dig1 := 0
else
dig1 := 11 - rest1;

if StrToInt(copy(EdtCnpj.Text,10,1))<> dig1 then
begin
ShowMessage(´CPF inválido´);
EdtCnpj.SetFocus;
exit
end;
tot := StrToInt(copy(EdtCnpj.Text,1,1))* 11 +
StrToInt(copy(EdtCnpj.Text,2,1))* 10 +
StrToInt(copy(EdtCnpj.Text,3,1))* 9 +
StrToInt(copy(EdtCnpj.Text,4,1))* 8 +
StrToInt(copy(EdtCnpj.Text,5,1))* 7 +
StrToInt(copy(EdtCnpj.Text,6,1))* 6 +
StrToInt(copy(EdtCnpj.Text,7,1))* 5 +
StrToInt(copy(EdtCnpj.Text,8,1))* 4 +
StrToInt(copy(EdtCnpj.Text,9,1))* 3 +
StrToInt(copy(EdtCnpj.Text,10,1))* 2 ;

rest2 := (tot mod 11);
if rest2 < 2 then
dig2 := 0
else
dig2 := 11 - rest2;
if StrToInt(copy(EdtCnpj.Text,11,1))<> dig2 then
begin
ShowMessage(´CPF inválido´);
EdtCnpj.SetFocus;
exit;
end;
{Colocando a mascara}
if not EdtCnpj.IsMasked then
EdtCnpj.EditMask := ´999.999.999-99;1;_´
end;

end;

- Se quiser pode controlar a digitação do usuário co a seguinte procedure:

procedure TFrmClientes.EdtCnpjKeyPress(Sender: TObject; var Key: Char);
begin
if not(key in [´0´..´9´,#8,13]) then {permite digitar numeros de 0 a 9 e o uso da tecla backspace e da tecla enter}
key := 0;
end;

Para evitar alguns inconvenientes é bom tirar a mascara quando digitar no objeto:

procedure TFrmClientes.EdtCnpjEnter(Sender: TObject);
begin
if EdtCnpj.IsMasked then
EdtCnpj.EditMask := ´´
end;


GOSTEI 0
POSTAR