How to Validate a Credit Card Number

This information is provided courtesy of Holotech Enterprises.

Note: The methods described below only verify that a card number is valid. The only way to know if a transaction will go through is to submit it to a payment processor.

Each card type begins with a certain digit or digits, and has a specific number of digits. The first step is to ensure the card number matches the type in this way.
Name # of Digits Starts With
MasterCard 16 51-55
Visa 13, 16 4
AmEx 15 34
37
Diners Club/
Carte Blanche
14 300-305
36
38
Discover 16 6011
enRoute 15 2014
2149
JCB 16 3
JCB 15 2131
1800
Delta 16 4
Switch 16, 18, 19 4-6

Once the number has passed this check, it must pass a check against the Luhn Mod 10 formula. This is a mathematical formula that all valid credit card numbers conform to.

To illustrate this formula, we'll use the number 8451 3097 5687 2042. This is not a real credit card number, but does conform to the Luhn formula. Starting with the last digit, work toward the beginning of the number, adding the digits together. Even-numbered digits are multiplied by two before adding. If the product of the multiplication is more than one digit, those digits are added to each other before adding to the total:

digit x2 add total
2     2
4 8   8
0     0
2 4   4
7     7
8 16 1 + 6 7
6     6
5 10 1 + 0 1
7     7
9 18 1 + 8 9
0     0
3 6   6
1     1
5 10 1 + 0 1
4     4
8 16 1 + 6 7
And the total is: 70

Add up all the numbers you obtained with the above calculations. If the result is evenly divisible by 10, the number is good. 70/10 = 7: no remainder.