DelphiFAQ Home Search:

Credit Card Validation

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

Are you in need to validate a credit card? The following routine does some basic checking and returns the type of the credit card as a number - or use the const array to get the type of credit card by name. (E.g. 'Mastercard').

This code does not check that the credit card is actually valid, that it is good for a purchase or whether it belongs to a certain person. To accept any kind of orders, you need to do an address verification, combined with checking the expiration date.

The routine is still handy as an input validator on forms. You may download it here.

program CardTest;

uses
  Dialogs,
  SysUtils;

{$R *.RES}

const
  CardType : array [0..4] of string = ('Invalid', 'Amex', 'Visa', 'Mastercard',
    'Discover' );

function Vc(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 
    Vc := Ord(Cstr[1])-Ord('2') 
  else 
    Vc := 0 
end; 

begin
  ShowMessage(CardType[Vc('4479750100222862')]);
end. 
You don't like the formatting? Check out SourceCoder then!

Comments:

 

 

Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option.
 
It seems that you are
from Washington, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:
photo Add a picture:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity. The owner of this web site reserves the right to delete such material.