top of page
Credit Card Number Validation
-
Most credit card number and Account numbers can be validated using the Luhn algorithm.
-
And many government identification numbers use the algorithm as a simple method of distinguishing valid numbers from mistyped or otherwise incorrect numbers.
WHAT IS LUHN ALGORITHM ?
-
It is a simple checksum formula used to validate a variety of identification numbers such as:
-
Credit Card Numbers.
-
Debit Card Number.
-
Bank Account Numbers.
-
IMEI Numbers.
-
Canadian Social Insurance Numbers.
-
Also known as the modulus 10 or mod 10 algorithm.
-
It was created by IBM scientist Hans Peter Luhn.
-
It was designed to protect against accidental errors, not malicious attacks.
HOW IT WORKS ?
-
Let’s understand the algorithm with an example.
Major Industry Identifier (MII) (eg. Banking, Airline, Travel)
1st digit :
Issuer Identification Number (eg. Visa, MasterCard, Amex)
1st - 6th digits :
Account Number
7th onward :
(excluding last digit)
Check Digits
Last digit :
1
2
3
4
-
Here are the Luhn steps which can used to validate the credit card number :
4000077358283885
Step 1 : Starting from the rightmost digit double the value of every second digit.
Step 2 :
If doubling of a number results in a two digits number i.e greater than 9 (e.g., 8 × 2 = 16), add up the two digits to get a single-digit number (like for 16:1+6, 18=1+8).
Step 3 :
Now add the un-doubled digits to the odd places.
Step 4 :
Now take the sum of all the digits.
Step 5 :
If the result from Step 4 is divisible by 10, the card number is valid; otherwise, it is invalid.
Double every other
(Right to left)
Add up the two digits
4 0 0 0 0 7 7 3 5 8 2 8 3 8 8 5
Drop the last digit.
(4x2)
(0x2)
(0x2)
(7x2)
(5x2)
(2x2)
(3x2)
(8x2)
8 0 0 14 10 4 6 16
(1+4)
(1+0)
(1+6)
8 0 0 5 1 4 6 7
(if any result higher than 9)
Add un-doubled digits
8 0 0 0 0 7 5 3 1 8 4 8 6 8 7 5
Sum of all nos. together
8+0+0+0+0+7+5+3+1+8+4+8+6+8+7+5 = 70
Molulo 10
(if result is 0, valid; otherwise invalid)
70 modulo 10 = 0
The card number is valid.
-
This algorithm also apply on :
-
Account numbers.
-
IMEI numbers.
-
Canadian Social Insurance Numbers.
WHY LUHN ALGORITHM ?
-
The Luhn algorithm detects any single-digit error, as well as almost all transpositions of adjacent digits.
-
When you implementing eCommerce application, It is a best practice validating credit card number before send it to the bank validation.
WEAKNESS
-
It will not detect transposition of the two-digit sequence 09 to 90 (or vice versa).
-
It will detect 7 of the 10 possible twin errors (it will not detect 22 ↔ 55, 33 ↔ 66 or 44 ↔ 77).
bottom of page