Checar se CNPJ ou CPF são válidos
Autor: Sama Assuero | Publicado: 2012-05-04T09:58:00-07:00

Funções para checar se o o CNPJ e o CPF são válidos. 

unit DVs;
{ DVs.Pas - Copyleft (1997) - Ivan C Cruz.

&nbsp; ivancruz@centroin.com.br
&nbsp; www.forumbr.com

&nbsp; VocÍ pode distribuir, utilizar e alterar livremente estas rotinas desde
&nbsp; que mantenha este coment·rio que identifica o autor original.
}
interface
function DvModulo11 ( str: String ): Char;
function DvModulo10 ( str: String ): Char;
function DvCGC ( str: String ): String;
function DvCPF ( str: String ): String;
function ValidaCGC ( str: String ): Boolean;
function ValidaCPF ( str: String ): Boolean;

implementation
{ chInt - Converte um caracter numÈrico para o valor inteiro correspondente. }
function chInt ( ch: Char ): ShortInt;
&nbsp; begin
&nbsp; &nbsp; Result := Ord ( ch ) - Ord ( '0' );
&nbsp; end;
{ intCh = Converte um valor inteiro (de 0 a 9) para o caracter numÈrico
&nbsp; correspondente. }
function intCh ( int: ShortInt ): Char;
&nbsp; begin
&nbsp; &nbsp; Result := Chr ( int + Ord ( '0' ) );
&nbsp; end;
{ DvModulo11 - Retorna 1 dv calculado pelo mÈtodo do modulo 11 padr„o. }
function DvModulo11 ( str: String ): Char;
&nbsp; var soma, fator, i: Integer;
&nbsp; begin
&nbsp; &nbsp; soma := 0;
&nbsp; &nbsp; fator := 2;
&nbsp; &nbsp; for i := Length ( str ) downto 1 do
&nbsp; &nbsp; &nbsp; begin
&nbsp; &nbsp; &nbsp; &nbsp; soma := soma + chInt ( str[i] ) * fator;
&nbsp; &nbsp; &nbsp; &nbsp; Inc ( fator );
&nbsp; &nbsp; &nbsp; &nbsp; if fator = 10 then
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fator := 2;
&nbsp; &nbsp; &nbsp; end;
&nbsp; &nbsp; soma := 11 - ( soma mod 11 );
&nbsp; &nbsp; if soma &gt;= 10 then
&nbsp; &nbsp; &nbsp; &nbsp; Result := '0'
&nbsp; &nbsp; else
&nbsp; &nbsp; &nbsp; &nbsp; Result := intCh ( soma );
&nbsp; end;
{ dvModulo11ParaCPF - Retorna 1 dv calculado pelo mÈtodo do modulo 11
&nbsp; ligeiramente alterado para o CPF. }
function dvModulo11ParaCPF ( str: String ): Char;
&nbsp; var soma, fator, i: Integer;
&nbsp; begin
&nbsp; &nbsp; soma := 0;
&nbsp; &nbsp; fator := 2;
&nbsp; &nbsp; for i := Length ( str ) downto 1 do
&nbsp; &nbsp; &nbsp; begin
&nbsp; &nbsp; &nbsp; &nbsp; soma := soma + chInt ( str[i] ) * fator;
&nbsp; &nbsp; &nbsp; &nbsp; Inc ( fator );
&nbsp; &nbsp; &nbsp; end;
&nbsp; &nbsp; soma := 11 - ( soma mod 11 );
&nbsp; &nbsp; if soma &gt;= 10 then
&nbsp; &nbsp; &nbsp; &nbsp; Result := '0'
&nbsp; &nbsp; else
&nbsp; &nbsp; &nbsp; &nbsp; Result := intCh ( soma );
&nbsp; end;
{ DvModulo10 - Retorna 1 dv calculado pelo mÈtodo do modulo 10 padr„o. }
function DvModulo10 ( str: String ): Char;
&nbsp; var soma, fator, i: Integer;
&nbsp; begin
&nbsp; &nbsp; soma := 0;
&nbsp; &nbsp; fator := 2;
&nbsp; &nbsp; for i := Length ( str ) downto 1 do
&nbsp; &nbsp; &nbsp; begin
&nbsp; &nbsp; &nbsp; &nbsp; soma := soma + chInt ( str[i] ) * fator;
&nbsp; &nbsp; &nbsp; &nbsp; Dec ( fator );
&nbsp; &nbsp; &nbsp; &nbsp; if fator = 0 then
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fator := 2;
&nbsp; &nbsp; &nbsp; end;
&nbsp; &nbsp; soma := 10 - ( soma div 11 );
&nbsp; &nbsp; if soma &gt;= 10 then
&nbsp; &nbsp; &nbsp; &nbsp; Result := '0'
&nbsp; &nbsp; else
&nbsp; &nbsp; &nbsp; &nbsp; Result := intCh ( soma );
&nbsp; end;
{ DvCGC - Retorna os dois dvs de um CGC, dado o CGC sem os dvs em forma de
&nbsp; string (12 caracteres numÈricos). }
function DvCGC ( str: String ): String;
&nbsp; var dv1: Char;
&nbsp; begin
&nbsp; &nbsp; dv1 := DvModulo11 ( str );
&nbsp; &nbsp; Result := dv1 + DvModulo11 ( str + dv1 );
&nbsp; end;
{ DvCPF - Retorna os dois dvs de um CPF, dado o CPF sem os dvs em forma de
&nbsp; string (9 caracteres numÈricos). }
function DvCPF ( str: String ): String;
&nbsp; var dv1: Char;
&nbsp; begin
&nbsp; &nbsp; dv1 := dvModulo11ParaCPF ( str );
&nbsp; &nbsp; Result := dv1 + dvModulo11ParaCPF ( str + dv1 );
&nbsp; end;
{ ValidaCGC - Retorna true se o CGC dado È valido. O CGC deve ser um string
&nbsp; de 14 caracteres numÈricos. }
function ValidaCGC ( str: String ): Boolean;
&nbsp; begin
&nbsp; &nbsp; Result := Copy ( str, 13, 2 ) = DvCGC ( Copy ( str, 1, 12 ) );
&nbsp; end;
{ ValidaCPF - Retorna true se o CPF dado È valido. O CPF deve ser um string
&nbsp; de 11 caracteres numÈricos. }
function ValidaCPF ( str: String ): Boolean;
&nbsp; begin
&nbsp; &nbsp; Result := Copy ( str, 10, 2 ) = DvCPF ( Copy ( str, 1, 9 ) );
&nbsp; end;
end.