Advertisement
HwapX

Validação CNPJ

Jan 29th, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.04 KB | None | 0 0
  1. function DigitoVerificadorCNPJ(const CNPJ: string): string;
  2. const
  3.   Mult : array[1..13] of Byte = (6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2);
  4. var
  5.   I : Integer;
  6.   Soma1 : Integer;
  7.   Soma2 : Integer;
  8.   Numero : Integer;
  9. begin
  10.   if Length(CNPJ) <> 12 then
  11.     Exit('');
  12.  
  13.   Soma1 := 0;
  14.   Soma2 := 0;
  15.  
  16.   for I := 1 to 12 do
  17.     if TryStrToInt(CNPJ[I], Numero) then
  18.     begin
  19.       Inc(Soma1, Numero * Mult[I+1]);
  20.       Inc(Soma2, Numero * Mult[I]);
  21.     end
  22.     else
  23.     begin
  24.       Exit('');
  25.       //raise Exception.Create('A string informada contem caracteres não numericos');
  26.     end;
  27.  
  28.   Result := '00';
  29.  
  30.   Numero := Soma1 mod 11;
  31.   if Numero > 1 then
  32.   begin
  33.     Numero    := 11 - Numero;
  34.     Result[1] := IntToStr(Numero)[1];
  35.     Inc(Soma2, Numero * Mult[13]);
  36.   end;
  37.  
  38.   Numero := Soma2 mod 11;
  39.   if Numero > 1 then
  40.     Result[2] := IntToStr(11 - Numero)[1];
  41. end;
  42.  
  43. function ValidaCNPJ(CNPJ : string): Boolean;
  44. begin
  45.   if Length(CNPJ) = 14 then
  46.     Result := DigitoVerificadorCNPJ(Copy(CNPJ, 1, 12)) = Copy(CNPJ, 13, 2);
  47. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement