Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to validade CNPJ or CPF Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
23-Jun-03
Category
Algorithm
Language
Delphi 2.x
Views
142
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tercio Ferdinando Gaudencio Filho

How to validade CNPJ or CPF

Answer:

In Brazil every people has a ID called CPF(Cadastro de pessoa fisica) and every 
company has a ID called CNPJ(Cadastro nacional de pessoa juridica). Some times we 
need to validate those IDs. 

1   //Validade CPF
2   
3   function ChkCPF(const cCPF: string): boolean;
4     function LimpaString(const StrNumerica: string): string;
5     var
6       i: integer;
7       valor: string;
8     begin
9       valor := StrNumerica;
10      for i := 1 to length(valor) do
11        if not (valor[i] in ['0'..'9']) then
12          Delete(valor, i, 1);
13      LimpaString := valor;
14    end;
15  
16    function CharToInt(cNum: char): integer;
17    begin
18      CharToInt := Ord(cNum) - 48;
19    end;
20  
21    function DigiSum(N: integer): integer;
22    var
23      value: integer;
24    begin
25      value := N mod 10 + N div 10;
26      if value >= 10 then
27        value := DigiSum(value);
28      DigiSum := value;
29    end;
30  var
31    i, soma, multiplo: integer;
32    CPF: string;
33  begin
34    ChkCPF := false;
35    CPF := LimpaString(cCPF);
36    if Length(CPF) <> 11 then
37      exit;
38    soma := 0;
39    for i := 9 downto 1 do
40    begin
41      soma := soma + CharToInt(CPF[i]) * (11 - i);
42    end;
43    multiplo := soma mod 11;
44    if multiplo <= 1 then
45      multiplo := 0
46    else
47      multiplo := 11 - multiplo;
48    if (multiplo <> CharToInt(CPF[10])) then
49      exit;
50    soma := 0;
51    for i := 10 downto 1 do
52    begin
53      soma := soma + CharToInt(CPF[i]) * (12 - i);
54    end;
55    multiplo := soma mod 11;
56    if multiplo <= 1 then
57      multiplo := 11;
58    ChkCPF := CharToInt(CPF[11]) = (11 - multiplo);
59  end;
60  
61  //Validade CNPJ
62  
63  function ChkCNPJ(const cCNPJ: string): boolean;
64    function LimpaString(const StrNumerica: string): string;
65    var
66      i: integer;
67      valor: string;
68    begin
69      valor := StrNumerica;
70      for i := 1 to length(valor) do
71        if not (valor[i] in ['0'..'9']) then
72          Delete(valor, i, 1);
73      LimpaString := valor;
74    end;
75  
76    function CharToInt(cNum: char): integer;
77    begin
78      CharToInt := Ord(cNum) - 48;
79    end;
80  
81    function DigiSum(N: integer): integer;
82    var
83      value: integer;
84    begin
85      value := N mod 10 + N div 10;
86      if value >= 10 then
87        value := DigiSum(value);
88      DigiSum := value;
89    end;
90  var
91    i, soma, mult: integer;
92    CGC: string;
93  begin
94    ChkCNPJ := false;
95    CGC := LimpaString(cCNPJ);
96    if Length(CGC) <> 14 then
97      exit;
98    soma := 0;
99    mult := 2;
100   for i := 12 downto 1 do
101   begin
102     soma := soma + CharToInt(CGC[i]) * mult;
103     mult := mult + 1;
104     if mult > 9 then
105       mult := 2;
106   end;
107   mult := soma mod 11;
108   if mult <= 1 then
109     mult := 0
110   else
111     mult := 11 - mult;
112   if mult <> CharToInt(CGC[13]) then
113     exit;
114   soma := 0;
115   mult := 2;
116   for i := 13 downto 1 do
117   begin
118     soma := soma + CharToInt(CGC[i]) * mult;
119     mult := mult + 1;
120     if mult > 9 then
121       mult := 2;
122   end;
123   mult := soma mod 11;
124   if mult <= 1 then
125     mult := 0
126   else
127     mult := 11 - mult;
128   ChkCNPJ := mult = CharToInt(CGC[14]);
129 end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC