Código Delphi
// Função: Validar CGC ²
// Testa se o CGC é Válido ou Não.
Código
function ValidaCGC(sCGC:string): boolean;
var CGCCalc: String;
i: integer;
Soma: integer;
Digito: integer;
begin
while Pos(' ', sCGC) > 0 do {converte espaços em zeros}
sCGC[Pos(' ', sCGC)] := '0';
while Length(sCGC) < 14 do
sCGC := '0' + sCGC;
CGCCalc := Copy(sCGC,1,12);
Soma := 0;
for i := 1 to 4 do
Soma := Soma + StrToInt(Copy(CGCCalc,i,1))*(6-i);
for i := 1 to 8 do
Soma := Soma + StrToInt(Copy(CGCCalc,i+4,1))*(10-i);
Digito := 11 - Soma mod 11;
if Digito in [10,11] then
CGCCalc := CGCCalc + '0'
else
CGCCalc := CGCCalc + IntToStr(Digito);
Soma := 0;
for i := 1 to 5 do
Soma := Soma + StrToInt(Copy(CGCCalc,i,1))*(7-i);
for i := 1 to 8 do
Soma := Soma + StrToInt(Copy(CGCCalc,i+5,1))*(10-i);
Digito := 11 - Soma mod 11;
if Digito in [10,11] then
CGCCalc := CGCCalc + '0'
else
CGCCalc := CGCCalc + IntToStr(Digito);
if sCGC <> CGCCalc then
Result := False
else
Result := True ; end;
var CGCCalc: String;
i: integer;
Soma: integer;
Digito: integer;
begin
while Pos(' ', sCGC) > 0 do {converte espaços em zeros}
sCGC[Pos(' ', sCGC)] := '0';
while Length(sCGC) < 14 do
sCGC := '0' + sCGC;
CGCCalc := Copy(sCGC,1,12);
Soma := 0;
for i := 1 to 4 do
Soma := Soma + StrToInt(Copy(CGCCalc,i,1))*(6-i);
for i := 1 to 8 do
Soma := Soma + StrToInt(Copy(CGCCalc,i+4,1))*(10-i);
Digito := 11 - Soma mod 11;
if Digito in [10,11] then
CGCCalc := CGCCalc + '0'
else
CGCCalc := CGCCalc + IntToStr(Digito);
Soma := 0;
for i := 1 to 5 do
Soma := Soma + StrToInt(Copy(CGCCalc,i,1))*(7-i);
for i := 1 to 8 do
Soma := Soma + StrToInt(Copy(CGCCalc,i+5,1))*(10-i);
Digito := 11 - Soma mod 11;
if Digito in [10,11] then
CGCCalc := CGCCalc + '0'
else
CGCCalc := CGCCalc + IntToStr(Digito);
if sCGC <> CGCCalc then
Result := False
else
Result := True ; end;