Buscar

Testar CGC e testar CPF

Código

(*

Eu uso um form só pra funções

Então se vc usar um só para funções deverá colocar no use o nome do formulário (o nome que estiver no objecto inspector na aba name)

No arquivo onde vai ficar a função colocar
assim apos o public:

*)



 public
    { Public declarations }
  end;
function vercpf(cpf:string):Boolean;
function TestaCgc(xCGC: String):Boolean;

(*

E apos o implementation coloque a função

*)


{$R *.dfm}

function TestaCgc(xCGC: String):Boolean;
{Testa se o CGC é válido ou não}
Var

d1,d4,xx,nCount,fator,resto,digito1,digito2 : Integer;
Check : String;
begin
xcgc := sonumeros(xcgc);
d1 := 0;
d4 := 0;
xx := 1;
for nCount := 1 to Length( xCGC )-2 do
    begin
    if Pos( Copy( xCGC, nCount, 1 ), '/-.' ) = 0 then
       begin
       if xx < 5 then
          begin
          fator := 6 - xx;
          end
       else
          begin
          fator := 14 - xx;
          end;
       d1 := d1 + StrToInt( Copy( xCGC, nCount, 1 ) ) * fator;
       if xx < 6 then
          begin
          fator := 7 - xx;
          end
       else
          begin
          fator := 15 - xx;
          end;
       d4 := d4 + StrToInt( Copy( xCGC, nCount, 1 ) ) * fator;
       xx := xx+1;
       end;
    end;
    resto := (d1 mod 11);
    if resto < 2 then
       begin
       digito1 := 0;
       end
   else
       begin
       digito1 := 11 - resto;
       end;
   d4 := d4 + 2 * digito1;
   resto := (d4 mod 11);
   if resto < 2 then
      begin
      digito2 := 0;
      end
   else
      begin
      digito2 := 11 - resto;
      end;
   Check := IntToStr(Digito1) + IntToStr(Digito2);
   if Check <> copy(xCGC,succ(length(xCGC)-2),2) then
      begin
      Result := False;
      end
   else
      begin
      Result := True;
      end;
end;


(*

Como usar esta função??

Para pessoas que sacam mesmo vão dizer que esta dica não tem tanta importância. Mas na verdade quem está começando como eu, fica perdido e não sabe como usar

Coloque um componente seja Edit, dbedit ou um MaskEdit1,com a opção de TEXT como ___.___.___-__,  e um botao para executar o teste, e um label.

Clique duas vezes no botao que vc colocou e digite:

*)


  if VerCpf(MaskEdit1.Text) = False then
    begin
      Label4.Caption:= 'CPF Incorreto';
    end
  else
    begin
      Label4.Caption:= 'CPF Correto';
    end;

(*

Se estiver correto os números ira aparecer a mensagem  'CPF Incorreto' se estiver errado 'CPF Correto'

Att
Roberto
rclaudio7@hotmail.com

*)

 

Publicidade

Vote na dica




Quantidade de votos: 1 voto
Aceitação: 20%


Detalhes da dica

Categoria: Arquivos
Adicionada dia: 30/09/09
Por: Roberto Claudio
Visualizada: 3304 vezes

Planeta Delphi - Tudo sobre programação Delphi Planeta Delphi - www.planetadelphi.com.br - Todos os direitos reservados | Copyright 2001-2009