melhorar o código para verificar CNPJ ou CPF

Delphi

12/05/2016

Boa tarde, algum do colegas poderia me ajudar a melhorar o código a baixo

procedure TFrmCadClientes.DBCNPJCPFExit(Sender: TObject);
var tam : integer;
begin
inherited;
tam := Length(trim(DBCNPJCPF.Text));

if FDCadPadrao.State in [ dsinsert, dsedit ] then
begin
If (tam <> 11) and (tam <> 14) then
Begin
Application.MessageBox('Por Favor Digitar um CPF ou CNPJ Válido!!', 'Aviso', MB_OK + MB_ICONINFORMATION);
DBCNPJCPF.SetFocus;
exit;
End
Else
begin
If Length(trim(DBCNPJCPF.Text)) = 11 then
begin
If checa_cgccpf(DBCNPJCPF.text) then
begin
BtnCNPJCPF.Enabled:= True;
FDCadPadraoCL_INSCRICAO.Value := 'ISENTO';
FDCadPadraoCL_INSC_MUNIC.Value:= 'ISENTO';
BtnCNPJCPF.SetFocus;
end;
end

else
begin
If Length(trim(DBCNPJCPF.Text)) = 14 then
begin
If checa_cgccpf(DBCNPJCPF.text) then
begin
BtnCNPJCPF.Enabled := True;
BtnCNPJCPF.SetFocus;
end;
end;
end;
end;
end;

end;

gostaria de diminuir
Emanuel Gonçalves

Emanuel Gonçalves

Curtidas 2

Respostas

Natanael Ferreira

Natanael Ferreira

12/05/2016

Creio não tenha muito o que melhorar no seu código, mas uma opção seria trocar os IF's por um Case:

procedure Tfrmmenu.DBCNPJCPFExit(Sender: TObject);
var
  tam: integer;
begin
  tam := Length(trim(DBCNPJCPF.Text));

  if FDCadPadrao.State in [dsinsert, dsedit] then
    case tam of
      11:
        If checa_cgccpf(DBCNPJCPF.Text) then
        begin
          BtnCNPJCPF.Enabled := True;
          FDCadPadraoCL_INSCRICAO.Value := 'ISENTO';
          FDCadPadraoCL_INSC_MUNIC.Value := 'ISENTO';
          BtnCNPJCPF.SetFocus;
        end;
      14:
        If checa_cgccpf(DBCNPJCPF.Text) then
        begin
          BtnCNPJCPF.Enabled := True;
          BtnCNPJCPF.SetFocus;
        end;
    else
      Application.MessageBox('Por Favor Digitar um CPF ou CNPJ Válido!!', 'Aviso',
        MB_OK + MB_ICONINFORMATION);
      DBCNPJCPF.SetFocus;
    end;
end;
GOSTEI 0
Emanuel Gonçalves

Emanuel Gonçalves

12/05/2016

obrigado
GOSTEI 0
POSTAR