Funções para checar se o o CNPJ e o CPF são válidos.
unit DVs; { DVs.Pas - Copyleft (1997) - Ivan C Cruz. ivancruz@centroin.com.br www.forumbr.com VocÍ pode distribuir, utilizar e alterar livremente estas rotinas desde 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; begin Result := Ord ( ch ) - Ord ( '0' ); end; { intCh = Converte um valor inteiro (de 0 a 9) para o caracter numÈrico correspondente. } function intCh ( int: ShortInt ): Char; begin Result := Chr ( int + Ord ( '0' ) ); end; { DvModulo11 - Retorna 1 dv calculado pelo mÈtodo do modulo 11 padr„o. } function DvModulo11 ( str: String ): Char; var soma, fator, i: Integer; begin soma := 0; fator := 2; for i := Length ( str ) downto 1 do begin soma := soma + chInt ( str[i] ) * fator; Inc ( fator ); if fator = 10 then fator := 2; end; soma := 11 - ( soma mod 11 ); if soma >= 10 then Result := '0' else Result := intCh ( soma ); end; { dvModulo11ParaCPF - Retorna 1 dv calculado pelo mÈtodo do modulo 11 ligeiramente alterado para o CPF. } function dvModulo11ParaCPF ( str: String ): Char; var soma, fator, i: Integer; begin soma := 0; fator := 2; for i := Length ( str ) downto 1 do begin soma := soma + chInt ( str[i] ) * fator; Inc ( fator ); end; soma := 11 - ( soma mod 11 ); if soma >= 10 then Result := '0' else Result := intCh ( soma ); end; { DvModulo10 - Retorna 1 dv calculado pelo mÈtodo do modulo 10 padr„o. } function DvModulo10 ( str: String ): Char; var soma, fator, i: Integer; begin soma := 0; fator := 2; for i := Length ( str ) downto 1 do begin soma := soma + chInt ( str[i] ) * fator; Dec ( fator ); if fator = 0 then fator := 2; end; soma := 10 - ( soma div 11 ); if soma >= 10 then Result := '0' else Result := intCh ( soma ); end; { DvCGC - Retorna os dois dvs de um CGC, dado o CGC sem os dvs em forma de string (12 caracteres numÈricos). } function DvCGC ( str: String ): String; var dv1: Char; begin dv1 := DvModulo11 ( str ); Result := dv1 + DvModulo11 ( str + dv1 ); end; { DvCPF - Retorna os dois dvs de um CPF, dado o CPF sem os dvs em forma de string (9 caracteres numÈricos). } function DvCPF ( str: String ): String; var dv1: Char; begin dv1 := dvModulo11ParaCPF ( str ); Result := dv1 + dvModulo11ParaCPF ( str + dv1 ); end; { ValidaCGC - Retorna true se o CGC dado È valido. O CGC deve ser um string de 14 caracteres numÈricos. } function ValidaCGC ( str: String ): Boolean; begin Result := Copy ( str, 13, 2 ) = DvCGC ( Copy ( str, 1, 12 ) ); end; { ValidaCPF - Retorna true se o CPF dado È valido. O CPF deve ser um string de 11 caracteres numÈricos. } function ValidaCPF ( str: String ): Boolean; begin Result := Copy ( str, 10, 2 ) = DvCPF ( Copy ( str, 1, 9 ) ); end; end.
0 comentários:
Postar um comentário