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:
- Reverse the card numbers.
- Double every second digit (starting with the second digit).
- Total all resulting individual digits. (Two-digit numbers are treated as separate digits)
- 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.
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.
Similar Entries
- PHP project: convert times to numbers (333 recent visits)
- Letters/Numbers puzzle (144 recent visits)
Stats
456 unique visits since August 2008
Recent Referrers (click)
- luhn javascript code
- luhn javascript code
- javascript to validate 16 digits
- what is algorithm validation java
- Credit Card Number Validate
- Credit Card Number Validate
- luhn check java
- Luhn's Algorithm for credit card validation
- javascript luhn
- javascript luhn
- javascript luhn
- luhn algorithm
- luhn algorithm
- luhn validation php
- Luhn Check + java
- Luhn algorithm with first number check in java
- luhn's algorithm javascript
- php validate card
- Luhn algorithm php