Validando Cartão de Crédito

 

A função CheckCC verifica se o número digitado identifica um cartão de crédito. Esta retorna um número inteiro e recebe como parâmetro uma string que é o número do cartão.

 

function TForm1.CheckCC(c: string): Integer;

var

  card: string[21];

  Vcard: array[0..21] of Byte absolute card;

  Xcard: Integer;

  Cstr: string[21];

  y, x: Integer;

begin

   Cstr := '';

   FillChar(Vcard, 22, #0);

   card := c;

   for x := 1 to 20 do

      if (Vcard[x] in [48..57]) then

         Cstr := Cstr + chr(Vcard[x]);

         card := '';

         card := Cstr;

         Xcard := 0;

         if not odd(Length(card)) then

            for x := (Length(card) - 1) downto 1 do

               begin

                  if odd(x) then

                     y := ((Vcard[x] - 48) * 2)

                  else

                     y := (Vcard[x] - 48);

                     if (y >= 10) then

                        y := ((y - 10) + 1);

                        Xcard := (Xcard + y)

                  end

                  else

                     for x := (Length(card) - 1) downto 1 do

                        begin

                           if odd(x) then

                              y := (Vcard[x] - 48)

                           else

                              y := ((Vcard[x] - 48) * 2);

                           if (y >= 10) then

                              y := ((y - 10) + 1);

                              Xcard := (Xcard + y)

                           end;

                           x := (10 - (Xcard mod 10));

                           if (x = 10) then

                              x := 0;

                           if (x = (Vcard[Length(card)] - 48)) then

                              Result := Ord(Cstr[1]) - Ord('2')

                           else

                              Result := 0

end; 

 

Veja abaixo a interface deste aplicativo.