SlideShare une entreprise Scribd logo
1  sur  2
Télécharger pour lire hors ligne
Validador de CPF no Delphi 7                         else
                                                                 SegundoDigito := 11 - Resto2;
                                                             if (PrimeiroDigito <>
                                                   StrToInt(Dado[10])) or
function TestaCPF(Dado : string) : boolean;                     (SegundoDigito <>
                                                   StrToInt(Dado[11])) then
var D1 : array[1..9] of byte;                                    Result := false;
   I, DF1, DF2, DF3, DF4, DF5, DF6, Resto1,              end;
Resto2, PrimeiroDigito, SegundoDigito : integer;      end
begin                                                 else
   Result := true;                                       if Length(Dado) <> 0 then
   if Length(Dado) = 11 then                                 Result := false;
   begin                                           end;
       for I := 1 to 9 do
           if Dado[I] in ['0'..'9'] then           function TestaCGC(Dado : string) : boolean;
               D1[I] := StrToInt(Dado[I])
           else                                    var D1: array[1..12] of byte;
               Result := false;                       I, DF1, DF2, DF3, DF4, DF5, DF6, Resto1,
       if Result then                              Resto2, PrimeiroDigito, SegundoDigito : integer;
       begin                                       begin
           DF1 := 0;                                  Result := true;
           DF2 := 0;                                  if Length(Dado) = 14 then
           DF3 := 0;                                  begin
           DF4 := 0;                                      for I := 1 to 12 do
           DF5 := 0;                                          if Dado[I] in ['0'..'9'] then
           DF6 := 0;                                              D1[I] := StrToInt(Dado[I])
           Resto1 := 0;                                       else
           Resto2 := 0;                                           Result := false;
           PrimeiroDigito := 0;                           if Result then
           SegundoDigito := 0;                            begin
           DF1 := 10*D1[1] + 9*D1[2] + 8*D1[3]                DF1 := 0;
+ 7*D1[4] + 6*D1[5] + 5*D1[6] + 4*D1[7] +                     DF2 := 0;
3*D1[8] + 2*D1[9];                                            DF3 := 0;
           DF2 := DF1 div 11;                                 DF4 := 0;
           DF3 := DF2 * 11;                                   DF5 := 0;
           Resto1 := DF1 - DF3;                               DF6 := 0;
           if (Resto1 = 0) or (Resto1 = 1) then               Resto1 := 0;
                                                              Resto2 := 0;
            PrimeiroDigito := 0                               PrimeiroDigito := 0;
        else                                                  SegundoDigito := 0;
            PrimeiroDigito := 11 - Resto1;                    DF1 := 5*D1[1] + 4*D1[2] + 3*D1[3] +
        DF4 := 11*D1[1] + 10*D1[2] +               2*D1[4] + 9*D1[5] + 8*D1[6] +
9*D1[3] + 8*D1[4] + 7*D1[5] + 6*D1[6] +                            7*D1[7] + 6*D1[8] + 5*D1[9] +
             5*D1[7] + 4*D1[8] + 3*D1[9] +         4*D1[10] + 3*D1[11] + 2*D1[12];
2*PrimeiroDigito;                                             DF2 := DF1 div 11;
        DF5 := DF4 div 11;                                    DF3 := DF2 * 11;
        DF6 := DF5 * 11;                                      Resto1 := DF1 - DF3;
        Resto2 := DF4 - DF6;                                  if (Resto1 = 0) or (Resto1 = 1) then
        if (Resto2 = 0) or (Resto2 = 1) then                      PrimeiroDigito := 0
            SegundoDigito := 0                                else
PrimeiroDigito := 11 - Resto1;            else MessageDlg('O número informado NÃO
          DF4 := 6*D1[1] + 5*D1[2] + 4*D1[3] +      representa um CGC' + #13 +
3*D1[4] + 2*D1[5] + 9*D1[6] +                                        'válido pelas regras da Receita
               8*D1[7] + 7*D1[8] + 6*D1[9] +        Federal', mtWarning, [mbOK], 0);
5*D1[10] + 4*D1[11] + 3*D1[12] +                     end;
               2*PrimeiroDigito;                     if length(Dado) = 11 then begin
          DF5 := DF4 div 11;                            if TestaCPF(Dado) then begin
          DF6 := DF5 * 11;                                 insert('-',Dado,10);
          Resto2 := DF4 - DF6;                             insert('.',Dado,7);
          if (Resto2 = 0) or (Resto2 = 1) then             insert('.',Dado,4);
              SegundoDigito := 0                        end
          else                                          else MessageDlg('O número informado NÃO
              SegundoDigito := 11 - Resto2;         representa um CPF' + #13 +
          if (PrimeiroDigito <>                                      'válido pelas regras da Receita
StrToInt(Dado[13])) or                              Federal', mtWarning, [mbOK], 0);
             (SegundoDigito <>                       end;
StrToInt(Dado[14])) then                             Result := Dado;
              Result := false;                      end;
      end;
   end                                              {$R *.dfm}
   else                                             procedure TForm1.Button1Click(Sender:
      if Length(Dado) <> 0 then                     TObject);
                                                    begin
         Result := false;                           edit1.Text := TestaCpfCgc(edit1.Text)
                                                    //edit1.Text := CPF(edit1.Text)
end;                                                end;

function TestaCpfCgc(Dado: string): String;         procedure TForm1.Button2Click(Sender:
                                                    TObject);
var                                                 begin
 i: integer;                                        edit2.Text := TestaCpfCgc(edit2.Text)
begin                                               end;
 for i:= 1 to length(Dado) do begin
   if not (Dado[i] in ['0'..'9']) then
delete(Dado,i,1);
 end;
 if ((length(Dado) <> 11) and (length(Dado) <>
14))then
    MessageDlg('ATENÇÃO: O número
informado NÃO representa' + #13 +
            'um CPF ou CGC válido pelas regras da
Receita Federal',mtWarning,[mbOK],0);
 if length(Dado) = 14 then begin
    if TestaCGC(Dado) then begin
       insert('-',Dado,13);
       insert('/',Dado,9);
       insert('.',Dado,6);
       insert('.',Dado,3);
    end

Contenu connexe

Plus de Elifranio Alves Cruz (6)

Apostila php-avancado1444
Apostila php-avancado1444Apostila php-avancado1444
Apostila php-avancado1444
 
Tutoriais e dicas
Tutoriais e dicasTutoriais e dicas
Tutoriais e dicas
 
Realidadevirtual
RealidadevirtualRealidadevirtual
Realidadevirtual
 
Apostila banco de dados
Apostila banco de dadosApostila banco de dados
Apostila banco de dados
 
Ado
AdoAdo
Ado
 
Hibernate conceitos
Hibernate conceitosHibernate conceitos
Hibernate conceitos
 

Validador CPF CGC Delphi

  • 1. Validador de CPF no Delphi 7 else SegundoDigito := 11 - Resto2; if (PrimeiroDigito <> StrToInt(Dado[10])) or function TestaCPF(Dado : string) : boolean; (SegundoDigito <> StrToInt(Dado[11])) then var D1 : array[1..9] of byte; Result := false; I, DF1, DF2, DF3, DF4, DF5, DF6, Resto1, end; Resto2, PrimeiroDigito, SegundoDigito : integer; end begin else Result := true; if Length(Dado) <> 0 then if Length(Dado) = 11 then Result := false; begin end; for I := 1 to 9 do if Dado[I] in ['0'..'9'] then function TestaCGC(Dado : string) : boolean; D1[I] := StrToInt(Dado[I]) else var D1: array[1..12] of byte; Result := false; I, DF1, DF2, DF3, DF4, DF5, DF6, Resto1, if Result then Resto2, PrimeiroDigito, SegundoDigito : integer; begin begin DF1 := 0; Result := true; DF2 := 0; if Length(Dado) = 14 then DF3 := 0; begin DF4 := 0; for I := 1 to 12 do DF5 := 0; if Dado[I] in ['0'..'9'] then DF6 := 0; D1[I] := StrToInt(Dado[I]) Resto1 := 0; else Resto2 := 0; Result := false; PrimeiroDigito := 0; if Result then SegundoDigito := 0; begin DF1 := 10*D1[1] + 9*D1[2] + 8*D1[3] DF1 := 0; + 7*D1[4] + 6*D1[5] + 5*D1[6] + 4*D1[7] + DF2 := 0; 3*D1[8] + 2*D1[9]; DF3 := 0; DF2 := DF1 div 11; DF4 := 0; DF3 := DF2 * 11; DF5 := 0; Resto1 := DF1 - DF3; DF6 := 0; if (Resto1 = 0) or (Resto1 = 1) then Resto1 := 0; Resto2 := 0; PrimeiroDigito := 0 PrimeiroDigito := 0; else SegundoDigito := 0; PrimeiroDigito := 11 - Resto1; DF1 := 5*D1[1] + 4*D1[2] + 3*D1[3] + DF4 := 11*D1[1] + 10*D1[2] + 2*D1[4] + 9*D1[5] + 8*D1[6] + 9*D1[3] + 8*D1[4] + 7*D1[5] + 6*D1[6] + 7*D1[7] + 6*D1[8] + 5*D1[9] + 5*D1[7] + 4*D1[8] + 3*D1[9] + 4*D1[10] + 3*D1[11] + 2*D1[12]; 2*PrimeiroDigito; DF2 := DF1 div 11; DF5 := DF4 div 11; DF3 := DF2 * 11; DF6 := DF5 * 11; Resto1 := DF1 - DF3; Resto2 := DF4 - DF6; if (Resto1 = 0) or (Resto1 = 1) then if (Resto2 = 0) or (Resto2 = 1) then PrimeiroDigito := 0 SegundoDigito := 0 else
  • 2. PrimeiroDigito := 11 - Resto1; else MessageDlg('O número informado NÃO DF4 := 6*D1[1] + 5*D1[2] + 4*D1[3] + representa um CGC' + #13 + 3*D1[4] + 2*D1[5] + 9*D1[6] + 'válido pelas regras da Receita 8*D1[7] + 7*D1[8] + 6*D1[9] + Federal', mtWarning, [mbOK], 0); 5*D1[10] + 4*D1[11] + 3*D1[12] + end; 2*PrimeiroDigito; if length(Dado) = 11 then begin DF5 := DF4 div 11; if TestaCPF(Dado) then begin DF6 := DF5 * 11; insert('-',Dado,10); Resto2 := DF4 - DF6; insert('.',Dado,7); if (Resto2 = 0) or (Resto2 = 1) then insert('.',Dado,4); SegundoDigito := 0 end else else MessageDlg('O número informado NÃO SegundoDigito := 11 - Resto2; representa um CPF' + #13 + if (PrimeiroDigito <> 'válido pelas regras da Receita StrToInt(Dado[13])) or Federal', mtWarning, [mbOK], 0); (SegundoDigito <> end; StrToInt(Dado[14])) then Result := Dado; Result := false; end; end; end {$R *.dfm} else procedure TForm1.Button1Click(Sender: if Length(Dado) <> 0 then TObject); begin Result := false; edit1.Text := TestaCpfCgc(edit1.Text) //edit1.Text := CPF(edit1.Text) end; end; function TestaCpfCgc(Dado: string): String; procedure TForm1.Button2Click(Sender: TObject); var begin i: integer; edit2.Text := TestaCpfCgc(edit2.Text) begin end; for i:= 1 to length(Dado) do begin if not (Dado[i] in ['0'..'9']) then delete(Dado,i,1); end; if ((length(Dado) <> 11) and (length(Dado) <> 14))then MessageDlg('ATENÇÃO: O número informado NÃO representa' + #13 + 'um CPF ou CGC válido pelas regras da Receita Federal',mtWarning,[mbOK],0); if length(Dado) = 14 then begin if TestaCGC(Dado) then begin insert('-',Dado,13); insert('/',Dado,9); insert('.',Dado,6); insert('.',Dado,3); end