Luhn Algorithm: Checker, Calculator and Explanation

The Luhn algorithm, also called mod 10, is the checksum that validates credit card numbers. Starting from the rightmost digit, double every second digit, subtract 9 from any result above 9, and add all the digits together. The number is valid if the total is a multiple of 10.

Luhn checker, step by step

Digit4242424242424242
Doubled?Γ—2–×2–×2–×2–×2–×2–×2–×2–
Value8282828282828282

Sum = 80. It is a multiple of 10, so the number passes the Luhn check.

Luhn check digit calculator

Enter every digit except the last one and get the check digit that makes the number valid.

Check digit: 2 β†’ full number4242424242424242

How the Luhn algorithm works

  1. Start from the rightmost digit, the check digit, and move left.
  2. Double every second digit: the second from the right, the fourth, the sixth, and so on.
  3. If doubling gives a two-digit number, subtract 9 from it. For example, 7 doubled is 14, which becomes 5. This is the same as adding the two digits, 1 + 4.
  4. Add up all the digits: the doubled ones after step 3 and the ones you left alone.
  5. If the total ends in 0, meaning it is divisible by 10, the number is valid.

Worked example: 4242 4242 4242 4242

Reading from the right, the digits alternate 2, 4, 2, 4 and so on. Every 4 is in a doubled position and becomes 8, while every 2 stays as it is. That gives eight 8s and eight 2s: 64 + 16 = 80. Since 80 is a multiple of 10, the number passes. It is Stripe's standard test card.

Calculating the check digit

To create a valid number, apply the same steps to the number without its last digit, remembering that the doubling now starts with its rightmost digit. Take the total modulo 10; the check digit is 10 minus that remainder, or 0 if the remainder is already 0. Every number from the card generator ends in a check digit calculated this way.

What it catches, and what it does not

Luhn catches every single-digit mistake and almost every swap of two neighbouring digits. The exception is 09 written as 90, or the reverse. It misses some "twin" errors, such as 22 typed as 55, and any change that happens to keep the total the same.

It is an error check, not a security feature. Anyone can produce a number that passes it, so a valid Luhn result says nothing about whether a card exists or has funds. Besides payment cards, the same checksum protects IMEI numbers on mobile phones and Canadian Social Insurance Numbers. It is named after Hans Peter Luhn, the IBM scientist who patented it in 1960.

Luhn algorithm in code

Luhn algorithm in JavaScript

function isValidLuhn(number) {
  const digits = number.replace(/\D/g, '');
  let sum = 0;
  let double = false;
  for (let i = digits.length - 1; i >= 0; i--) {
    let d = Number(digits[i]);
    if (double) {
      d *= 2;
      if (d > 9) d -= 9;
    }
    sum += d;
    double = !double;
  }
  return digits.length > 0 && sum % 10 === 0;
}

isValidLuhn('4242 4242 4242 4242'); // true

Luhn plus format: what makes a card number valid

Every major card network uses the Luhn check digit. A number must also start with one of the network's prefixes and have the right length.

NetworkStarts withLength
Visa413, 16 or 19 (almost always 16)
Mastercard51–55, 2221–272016
American Express34, 3715
Discover6011, 644–649, 6516–19
Diners Club36, 300–305, 38–3914–19
JCB3528–358916–19
UnionPay6216–19
Maestro5018, 5020, 5038, 6304, 6759, 6761–676312–19
RuPay60, 65, 81, 82, 50816
Mir2200–220416–19

To check prefix, length and checksum in one go, use the credit card validator.

Luhn algorithm FAQ

What is the Luhn algorithm?

A checksum formula used to validate credit card numbers and other identifiers. From the right, every second digit is doubled (subtracting 9 from results over 9), all digits are added, and the number is valid if the total is divisible by 10.

Is the Luhn algorithm the same as mod 10?

Yes. It is also called the modulus 10 or mod 10 algorithm, because the final test is whether the sum leaves a remainder of 0 when divided by 10.

Does passing the Luhn check mean a credit card is real?

No. Luhn only catches typing mistakes. Generating a number that passes is trivial, so a valid result says nothing about whether a card exists, is active or has funds.

What errors does the Luhn algorithm detect?

All single-digit errors and almost all transpositions of adjacent digits, except 09 and 90. It does not catch some twin errors, such as 22 written as 55.

Who invented the Luhn algorithm?

Hans Peter Luhn, a computer scientist at IBM. His patent for it was granted in 1960, and it is now specified in ISO/IEC 7812 for payment card numbers.