Author: William Gerbert
Credit Card Validation
Answer:
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.
1 program CardTest;
2 3 uses4 Dialogs,
5 SysUtils;
6 7 {$R *.RES}8 9 const10 CardType: array[0..4] ofstring = ('Invalid', 'Amex', 'Visa', 'Mastercard',
11 'Discover');
12 13 function Vc(C: string): Integer;
14 var15 Card: string[21];
16 VCard: array[0..21] of Byte absolute Card;
17 XCard: Integer;
18 Cstr: string[21];
19 y,
20 x: Integer;
21 begin22 Cstr := '';
23 FillChar(VCard, 22, #0);
24 Card := C;
25 for x := 1 to 20 do26 if (VCard[x] in [48..57]) then27 Cstr := Cstr + Chr(VCard[x]);
28 Card := '';
29 Card := Cstr;
30 XCard := 0;
31 ifnot odd(Length(Card)) then32 for x := (Length(Card) - 1) downto 1 do33 begin34 if odd(x) then35 y := ((VCard[x] - 48) * 2)
36 else37 y := (VCard[x] - 48);
38 if (y >= 10) then39 y := ((y - 10) + 1);
40 XCard := (XCard + y)
41 end42 else43 for x := (Length(Card) - 1) downto 1 do44 begin45 if odd(x) then46 y := (VCard[x] - 48)
47 else48 y := ((VCard[x] - 48) * 2);
49 if (y >= 10) then50 y := ((y - 10) + 1);
51 XCard := (XCard + y)
52 end;
53 x := (10 - (XCard mod 10));
54 if (x = 10) then55 x := 0;
56 if (x = (VCard[Length(Card)] - 48)) then57 Vc := Ord(Cstr[1]) - Ord('2')
58 else59 Vc := 0
60 end;
61 62 begin63 ShowMessage(CardType[Vc('4479750100222862')]);
64 end.