Code: Detail

Delphi - (Portuguese)

Validar Cpf e Cnpj

Rotina para validação de cpf e cnpj

Last update: 11/12/2013
Delphi       
 
N/A
N/A
N/A
N/A
 

function TFrmPrinc.ValidaCPFCNPJ(Valor:String):String; 
  function cpf(num: string): boolean; 
  var 
    n1,n2,n3,n4,n5,n6,n7,n8,n9: integer; 
    d1,d2: integer; 
    digitado, calculado: string; 
  begin 
    n1:=StrToInt(num[1]); 
    n2:=StrToInt(num[2]); 
    n3:=StrToInt(num[3]); 
    n4:=StrToInt(num[4]); 
    n5:=StrToInt(num[5]); 
    n6:=StrToInt(num[6]); 
    n7:=StrToInt(num[7]); 
    n8:=StrToInt(num[8]); 
    n9:=StrToInt(num[9]); 
    d1:=n9*9+n8*8+n7*7+n6*6+n5*5+n4*4+n3*3+n2*2+n1*1; 
    d1:=(d1 mod 11); 
    if d1>=10 then 
      d1:=0; 
    d2:=d1*9+n9*8+n8*7+n7*6+n6*5+n5*4+n4*3+n3*2+n2*1+n1*0; 
    d2:=(d2 mod 11); 
    if d2>=10 then 
      d2:=0; 
    calculado:=inttostr(d1)+inttostr(d2); 
    digitado:=num[10]+num[11]; 
    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 
    n1:=StrToInt(num[1]); 
    n2:=StrToInt(num[2]); 
    n3:=StrToInt(num[3]); 
    n4:=StrToInt(num[4]); 
    n5:=StrToInt(num[5]); 
    n6:=StrToInt(num[6]); 
    n7:=StrToInt(num[7]); 
    n8:=StrToInt(num[8]); 
    n9:=StrToInt(num[9]); 
    n10:=StrToInt(num[10]); 
    n11:=StrToInt(num[11]); 
    n12:=StrToInt(num[12]); 
    d1:=n12*6+n11*7+n10*8+n9*9+n8*2+n7*3+n6*4+n5*5+n4*6+n3*7+n2*8+n1*9; 
    d1:=11-(d1 mod 11); 
    if d1>=10 then 
      d1:=0; 
    d2:=d1*5+n12*6+n11*7+n10*8+n9*9+n8*2+n7*3+n6*4+n5*5+n4*6+n3*7+n2*8+n1*9; 
    d2:=11-(d2 mod 11); 
    if d2>=10 then 
      d2:=0; 
    calculado:=inttostr(d2)+inttostr(d1); 
    digitado:=num[13]+num[14]; 
    if calculado=digitado then 
      CNPJ:=true 
    else 
      CNPJ:=false; 
  end; 
begin 
  if Length(Valor) > 0  then 
  begin 
    if Length(Valor) = 11 then 
    begin 
      if CPF(Valor) = false then 
      begin 
        if MessageDlg('CPF inválido, deseja continuar sem os dados do cliente?',mtConfirmation, [mbYes, mbNo], 0) = mrYes then 
          Result := 's' 
        else 
          Result := 'n'; 
      end 
      else 
        Result := Valor; 
    end 
    else 
      if Length(Valor) = 14 then 
      begin 
        if CNPJ(Valor) = false then 
        begin 
          if MessageDlg('CNPJ inválido, deseja continuar sem os dados do cliente?',mtConfirmation, [mbYes, mbNo], 0) = mrYes then 
            Result := 's' 
          else 
            Result := 'n'; 
        end 
        else 
          Result := Valor; 
      end 
      else 
        if MessageDlg('O número informado não tem 11 dígitos(CPF) ou 14 dígitos(CNPJ),#13 deseja continuar sem os dados do cliente?',mtConfirmation, [mbYes, mbNo], 0) = mrYes then 
          Result := 's' 
        else 
          Result := 'n'; 
  end 
  else 
    Result := 's'; 
end; 

Source: http://www.activedelphi.com.br/forum/viewtopic.php?t=47655&postdays=0&postorder=asc&start=45&sid=c5d
 
Users who have marked this routine as a favorite
 
 
 
The site ti4fun is not responsible for the content on sites for which you have external links

Articles, routines, tips, forums, blogs or any other content posted on ti4fun site is not tested and not validated, so you should test and validate any information collected on the ti4fun site before applying it to final use environment, such as example, production. the TI4FUN site is not responsible for quality or for any damages, direct, indirect or consequential, from use of any content posted by the authors in the site.

All content published on the ti4fun site is the responsibility of the author and do not necessarily express the views of the site ti4fun and its employees.