Luhn Algorithm Calculator - Validate Credit Card & IMEI
A free Luhn algorithm calculator. Validate credit card, IMEI, and ID numbers with the mod 10 checksum, or generate valid test numbers instantly online.
Luhn Algorithm Calculator
Check if your number passes the Luhn mod 10 validation
Documentation
What Is the Luhn Algorithm?
The Luhn algorithm, also called the mod 10 algorithm, is a formula that checks whether a string of digits could be a valid identification number. It is used to catch typing mistakes in credit card numbers, IMEI numbers on phones, and other ID codes before they cause an error. IBM engineer Hans Peter Luhn described the method in a patent filed in 1954.
The algorithm does not prove that a number is real or active. It only confirms that the digits follow the expected pattern. A card number can pass the Luhn check and still be fake, expired, or unfunded.
How to Calculate a Luhn Checksum
The check works on the digits of a number, read from right to left.
- Starting with the second digit from the right, double every other digit.
- If doubling a digit gives a number larger than 9, subtract 9 from it. (This is the same as adding the two digits of the result together, so 16 becomes 1 + 6 = 7, or equivalently 16 − 9 = 7.)
- Add up all the digits: the doubled ones (after the adjustment) and the ones that were left alone.
- If the total sum is divisible by 10, the number is valid. If not, it is invalid.
When a business issues new ID numbers, it uses the same steps in reverse. It picks all the digits except the last one, runs the check, and then chooses the final "check digit" so that the total comes out divisible by 10.
Worked Example: Validating a Card Number
Take the test number 4532015112830366.
- Digits from right to left: 6, 6, 3, 0, 3, 8, 2, 1, 1, 5, 1, 0, 2, 3, 5, 4
- Double every second digit: 6, 12, 3, 0, 3, 16, 2, 2, 1, 10, 1, 0, 2, 6, 5, 8
- Subtract 9 from any result over 9: 6, 3, 3, 0, 3, 7, 2, 2, 1, 1, 1, 0, 2, 6, 5, 8
- Add them up: 6+3+3+0+3+7+2+2+1+1+1+0+2+6+5+8 = 50
- 50 is divisible by 10, so the number is valid.
Worked Example: An Invalid Number
Take 490154203237517, a 15-digit number in the format IMEI numbers use.
Running the same steps gives a sum of 59. Since 59 is not divisible by 10, this number is invalid.
To turn it into a valid number, only the last digit needs to change. Testing each possible last digit shows that a final digit of 8 brings the sum to 60, which is divisible by 10. So 490154203237518 is a valid Luhn number, while every other last digit from 0 to 9 (except 8) produces an invalid one.
Where the Luhn Algorithm Is Used
- Payment cards. Visa, Mastercard, American Express, and other networks build their card numbers so the full number passes the Luhn check. Checkout forms can use this to catch a mistyped digit before contacting a payment processor.
- IMEI numbers. The 15-digit identifier assigned to phones and other cellular devices includes a Luhn check digit.
- Canadian Social Insurance Numbers. The 9-digit SIN uses the same check.
- U.S. National Provider Identifier (NPI). Healthcare providers in the United States get a 10-digit NPI validated with a Luhn-based check.
What the Luhn Algorithm Does Not Catch
The algorithm always catches a single mistyped digit, no matter which digit it is or where it appears. It also catches every adjacent-digit swap (like typing "39" instead of "93") except one specific case: swapping a 0 and a 9 next to each other, such as "09" becoming "90".
It also misses three "twin" errors, where a repeated pair of digits is replaced by a different repeated pair: 22 read as 55, 33 read as 66, and 44 read as 77. Because these gaps are known and narrow, the Luhn check is reliable for catching ordinary typing mistakes but is not treated as a complete error-detection system on its own.
Luhn Is Not a Security Check
Luhn was designed to catch accidental data-entry errors, not fraud. A number that passes the check is mathematically well-formed, nothing more. Real payment security depends on other layers added after the Luhn check, such as the card's CVV code, address verification, and 3-D Secure authentication. Luhn is simply the first, cheapest filter, run before those slower checks.
Validating a Number in JavaScript
1function isValidLuhn(number) {
2 const digits = number.replace(/\D/g, '').split('').map(Number);
3 let sum = 0;
4 let doubleDigit = false;
5 for (let i = digits.length - 1; i >= 0; i--) {
6 let d = digits[i];
7 if (doubleDigit) {
8 d *= 2;
9 if (d > 9) d -= 9;
10 }
11 sum += d;
12 doubleDigit = !doubleDigit;
13 }
14 return sum % 10 === 0;
15}
16Frequently Asked Questions
What is the Luhn algorithm used for?
It checks whether an identification number, such as a credit card number, IMEI number, or Canadian SIN, is internally consistent. It catches mistyped or transposed digits before they cause a processing error.
How accurate is the Luhn algorithm at catching errors?
It catches every single mistyped digit and almost every case of two adjacent digits being swapped. The only swap it misses is 0 and 9 next to each other. It also misses three specific "twin" substitutions: 22 for 55, 33 for 66, and 44 for 77.
Does a valid Luhn number mean the card is real?
No. The check only confirms the digits fit the expected pattern. It says nothing about whether the card exists, is active, or has funds. Businesses add separate checks, such as a CVV code, for that.
Can the Luhn check be done without a computer?
Yes. It only needs doubling, subtraction, and addition, so it can be done by hand or with a simple calculator. This was part of the original design, meant for the punch-card era before digital computers were common.
Why is it called the mod 10 algorithm?
Because the final step checks whether the sum is evenly divisible by 10, which in mathematics is written as "sum mod 10 = 0". "Mod" is short for modulo, the remainder left after division.
Who invented the Luhn algorithm?
Hans Peter Luhn, an engineer at IBM, described it in a patent application filed in 1954 (granted in 1960 as US Patent 2,950,048). He is also known for early work on computerized text indexing.