Credit Card Check Digit Validation

The LUHN formula, also known as the modulus 10 algorithm, was created in the late 1960s. Shortly thereafter, credit card companies started using it. Because the algorithm is in the public domain, it can be used by anyone. Currently it's used by credit card companies, libraries, pharmacies, many motor vehicle divisions, various banks throughout the world, as well as the Canadian Social Insurance Number (SIN). Almost any institution that needs a unique account or identification numbers use the LUHN algorithm.

Let's use the following numbers to work through the math:

First remove any non numeric characters, and reverse the string.

Start with the second digit, double every other number.

Take the sum of every digit.

Divide the total by 10, if the remainder is 0 then the number is valid.
60 / 10 = 6 remainder 0, therefore 2323-2005-7766-3554 is valid

Now that we have the basic formula, credit card companies take it a step further and apply the following rules

Card Type Prefix(es) Length(s)
MasterCard 51-55 16
Visa 4 13 or 16
American Express 34 or 37 15
Discover 6011 16
Diners Club / Carte Blanche 300-305, 36, or 38 14

Toolbox - Luke Spence