Saturday, July 3, 2010

How to Test for Credit Card Numbers?

Ever wonder if there is a method to the madness of credit card numbers? In fact, the series of digits are arranged in a way that makes validation quite simple.

Most major credit cards in the U.S. follow what is known as the Luhn algorithm. Starting from the right, multiply every odd digit by 1, and every even digit by 2. If any of the products is greater than 9, subtract 9 to arrive at a single digit number. Then sum all the digits. The result should yield a multiple of 10.

Let’s test this:

Next time you are handed a credit card number simply follow the Luhn’s formula. If you get a multiple of 10, the card is valid.

1 comment: