



Muitas vezes é necessário a validação das informações preenchidas,
o CNPJ é um caso comum de validação.
Abaixo segue uma função para verificar se um CNPJ é válido:
É necessário declarar Math e SysUtils,
em versões unicode declare System.Math e System.SysUtils.
Abaixo o código fonte da função:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | function IsValidCNPJ(pCNPJ : string) : Boolean; var v: array[1..2] of Word; cnpj: array[1..14] of Byte; I: Byte; begin Result := False; { Verificando se tem 11 caracteres } if Length(pCNPJ) <> 14 then begin Exit; end; { Conferindo se todos dígitos são iguais } if pCNPJ = StringOfChar('0', 14) then Exit; if pCNPJ = StringOfChar('1', 14) then Exit; if pCNPJ = StringOfChar('2', 14) then Exit; if pCNPJ = StringOfChar('3', 14) then Exit; if pCNPJ = StringOfChar('4', 14) then Exit; if pCNPJ = StringOfChar('5', 14) then Exit; if pCNPJ = StringOfChar('6', 14) then Exit; if pCNPJ = StringOfChar('7', 14) then Exit; if pCNPJ = StringOfChar('8', 14) then Exit; if pCNPJ = StringOfChar('9', 14) then Exit; try for I := 1 to 14 do cnpj[i] := StrToInt(pCNPJ[i]); //Nota: Calcula o primeiro dígito de verificação. v[1] := 5*cnpj[1] + 4*cnpj[2] + 3*cnpj[3] + 2*cnpj[4]; v[1] := v[1] + 9*cnpj[5] + 8*cnpj[6] + 7*cnpj[7] + 6*cnpj[8]; v[1] := v[1] + 5*cnpj[9] + 4*cnpj[10] + 3*cnpj[11] + 2*cnpj[12]; v[1] := 11 - v[1] mod 11; v[1] := IfThen(v[1] >= 10, 0, v[1]); //Nota: Calcula o segundo dígito de verificação. v[2] := 6*cnpj[1] + 5*cnpj[2] + 4*cnpj[3] + 3*cnpj[4]; v[2] := v[2] + 2*cnpj[5] + 9*cnpj[6] + 8*cnpj[7] + 7*cnpj[8]; v[2] := v[2] + 6*cnpj[9] + 5*cnpj[10] + 4*cnpj[11] + 3*cnpj[12]; v[2] := v[2] + 2*v[1]; v[2] := 11 - v[2] mod 11; v[2] := IfThen(v[2] >= 10, 0, v[2]); //Nota: Verdadeiro se os dígitos de verificação são os esperados. Result := ((v[1] = cnpj[13]) and (v[2] = cnpj[14])); except on E: Exception do Result := False; end; end; |
Exemplo de uso:
1 2 3 4 5 6 7 8 | // Exemplo de uso: procedure TForm1.Button1Click(Sender: TObject); begin if IsValidCNPJ('54771614000104') then ShowMessage('O CNPJ é válido.') else ShowMessage('O CNPJ não é válido.'); end; |
Como alternativa pode-se citar o componente TACBrValidador que valida diversos
documentos e pertence a biblioteca do ACBr que é Open Source.
A fim de ajudar no entendimento, resolvemos colocar todo o fonte da unit de exemplo.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls; type TForm1 = class(TForm) Button1: TButton; EdtCNPJ: TLabeledEdit; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation uses System.Math; function IsValidCNPJ(pCNPJ : string) : Boolean; var v: array[1..2] of Word; cnpj: array[1..14] of Byte; I: Byte; begin Result := False; { Verificando se tem 11 caracteres } if Length(pCNPJ) <> 14 then begin Exit; end; { Conferindo se todos dígitos são iguais } if pCNPJ = StringOfChar('0', 14) then Exit; if pCNPJ = StringOfChar('1', 14) then Exit; if pCNPJ = StringOfChar('2', 14) then Exit; if pCNPJ = StringOfChar('3', 14) then Exit; if pCNPJ = StringOfChar('4', 14) then Exit; if pCNPJ = StringOfChar('5', 14) then Exit; if pCNPJ = StringOfChar('6', 14) then Exit; if pCNPJ = StringOfChar('7', 14) then Exit; if pCNPJ = StringOfChar('8', 14) then Exit; if pCNPJ = StringOfChar('9', 14) then Exit; try for I := 1 to 14 do cnpj[i] := StrToInt(pCNPJ[i]); //Nota: Calcula o primeiro dígito de verificação. v[1] := 5*cnpj[1] + 4*cnpj[2] + 3*cnpj[3] + 2*cnpj[4]; v[1] := v[1] + 9*cnpj[5] + 8*cnpj[6] + 7*cnpj[7] + 6*cnpj[8]; v[1] := v[1] + 5*cnpj[9] + 4*cnpj[10] + 3*cnpj[11] + 2*cnpj[12]; v[1] := 11 - v[1] mod 11; v[1] := IfThen(v[1] >= 10, 0, v[1]); //Nota: Calcula o segundo dígito de verificação. v[2] := 6*cnpj[1] + 5*cnpj[2] + 4*cnpj[3] + 3*cnpj[4]; v[2] := v[2] + 2*cnpj[5] + 9*cnpj[6] + 8*cnpj[7] + 7*cnpj[8]; v[2] := v[2] + 6*cnpj[9] + 5*cnpj[10] + 4*cnpj[11] + 3*cnpj[12]; v[2] := v[2] + 2*v[1]; v[2] := 11 - v[2] mod 11; v[2] := IfThen(v[2] >= 10, 0, v[2]); //Nota: Verdadeiro se os dígitos de verificação são os esperados. Result := ((v[1] = cnpj[13]) and (v[2] = cnpj[14])); except on E: Exception do Result := False; end; end; {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin if IsValidCNPJ(EdtCNPJ.Text) then ShowMessage('O CNPJ é válido.') else ShowMessage('O CNPJ não é válido.'); end; end. |
Você precisa fazer o login para publicar um comentário.
NAO FUNCIONA
Função atualizada e testada.
Adicionalmente colocamos todo o fonte da unit de exemplo para facilitar o entendimento.