DelphiFAQ Home Search:
General :: Programming :: Delphi
General Delphi related information.

Articles:

This list is sorted by recent document popularity (not total page views).
New documents will first appear at the bottom.
Recommended links on this topic:
Featured Article

Credit Card Validation

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!
Generated 4:02:26 on Aug 18, 2019