Skip to content

Instantly share code, notes, and snippets.

@reinaldorauch
Created August 19, 2014 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reinaldorauch/011ac7f09de7df0c05c3 to your computer and use it in GitHub Desktop.
Save reinaldorauch/011ac7f09de7df0c05c3 to your computer and use it in GitHub Desktop.
function TFmPrinc.validaCartaoCredito(cn: String): ShortInt;
var
i, j, Sum : Integer;
begin
if (Length(cn) > 20) OR (Length(cn) < 15) then
Result := -1
else
try
Sum := 0;
for i := Length(cn) - 1 downto 1 do
begin
j := Length(cn) - i;
if(j mod 2 = 1) then
inc(sum, StrToInt(cn[i]))
else
begin
J := StrToInt(cn[i]) * 2;
if(J > 9) then
inc(sum, (StrToInt(IntToStr(J)[1]) + StrToInt(IntToStr(J)[2])) * 2)
else
inc(sum, J);
end;
end;
if((10 - (Sum mod 10)) = StrToInt(cn[Length(cn)])) then
Result := 1
else
Result := -1;
except
Result := -1;
end;
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment