Validation of Credit Card Numbers



The last digit in a credit card number is what is called a checksum. It is chosen so that a value derived from the credit card number is evenly divisible by 10.

The other digits represent the credit card company, the bank that issued the card, and the customer account number.

Here are some rules for 3 different types of credit cards. We will only consider these types of cards. 

Card
Prefix
Length of Credit Card Number
Visa
4
16
Master Card
51-55
16
American Express
34 or 37
15



The Luhn Algorithm is used to check a credit card number's validity. Given a credit card number:

1. Reverse the order of the digits, removing non-digits (i.e., dashes).
2. In this list of reversed digits, double every other number, starting with the second number in the list.
3. Now, compute the sum of every single digit in the list. That is, if one of the numbers in your list is 14, add 1+4 to your accumulating sum.
4. If the sum you calculated in step 3 is divisible by 10, the number is valid. Otherwise, it is invalid.



Source:
1. http://www.brainjar.com/js/validation/default2.asp
2. http://en.wikipedia.org/wiki/Credit_card_numbers