Validating credit card numbers: The Luhn algorithm Dec22 '05

It’s interesting how credit card numbers are validated.

Below is a description of the formula used to validate a credit card number.

Note: "validate" is different from "authenticate." Validate simply means check to see if the numbers represent a valid credit card number, much like a U.S. zip code is always five numbers. "Validating" has nothing to do with the transaction, or the name on the card - it simply checks (up front) if the credit card number can even be submitted for authentication.

How it’s done

Here’s how it’s done:

  1. Reverse the card numbers.
  2. Double every second digit (starting with the second digit).
  3. Total all resulting individual digits. (Two-digit numbers are treated as separate digits)
  4. If the sum is divisible by 10, then the credit card number is valid.

Examining the process

Here’s an example credit card number, that I just made up (maybe I’ll get lucky and it will validate):

5883 3245 7781 0092

Let’s do the formula on this number.

First, we reverse the entire string of numbers:

2900 1877 5423 3885

Next, we double every second digit, starting with the second digit:

18, 0, 16, 14, 8, 6, 16, 10

To clarify that step - I started with the number 9, which is the second number from the left. I doubled that number. The next number to double is 0 - which is the last digit of the first four numbers (2900). I doubled that number, then I went to the number 8. I continued this trend until the end of all of the digits.

The next step is to add all of the "duplicate" digits together.

Again, our duplicate digits are:

18, 0, 16, 14, 8, 6, 16, 10

We need to add all of these digits together - but remember to treat each number as a single digit. So, this is how we would add them together:

1 + 8 + 0 + 1 + 6 + 1 + 4 + 8 + 6 + 1 + 6 + 1 + 0

Make sense? Thought so...

The sum of these numbers is 43.

According to our rule, if the number is divisible by 10, it’s valid. 43 is definitely not divisible by 10 - so the credit card number is not valid.

Practical applications

Since credit card numbers are used widely on the web, it makes sense to have a script check to see if numbers are valid. This can be done with a server-side scripting language (such as PHP, ASP), or more practically (I think) is with JavaScript.

The following function was extracted from a helpful JavaScript book that I use as my general reference book: Special Edition: Using JavaScript, by Paul McFedries.

Other information for this article was obtained at Wikipedia.

Categories: JavaScript , Tutorials

Add Feedback (view all)

Leave feedback

Feedback

Input format: The editor controls below will assist with Markdown syntax.

Status

Sub-status

Your info

Wow. That's really cool! Thanks for posting this! ... Read more.

That's crazy, cheers for the post. I always thought credit card validation would be a really complex process but that's just simple. I'll keep this ... Read more.

matthom is published and produced by Matt Thommes - an independent publishing enthusiast, mobile blogger, content creator, informative writer, web developer from Chicago. Never one to conform, Matt intends to promote the effect the web has on our lives, in an effort to intensify, instruct, and clarify all that is happening around us.

Contact Matt

Similar Entries

Stats

456 unique visits since August 2008

Syndicate

Advertisements