Technical

Understanding the Luhn Algorithm: The Math Behind Card Validation

October 15, 2025 · 6 min read · By Yasir Khan

← Back to Blog

Almost every payment form you have ever used quietly runs a small piece of arithmetic before it lets you press the pay button. That arithmetic is the Luhn algorithm, also called the modulus 10 or mod 10 check. It is not encryption and it is not a security feature. It is a checksum designed to catch honest typing mistakes, and it does that job remarkably well for something so simple.

Where the algorithm came from

Hans Peter Luhn, a computer scientist at IBM, filed the patent for the method in 1954. He wanted a cheap way to detect accidental errors when people copied long numbers by hand, and he wanted it to work without any lookup table. The patent has long since expired, and the method is described in the ISO/IEC 7812 standard for identification cards.

Today the same check appears well beyond payment cards. It is used for IMEI numbers on mobile phones, Canadian Social Insurance Numbers, and many internal account or membership numbers. Whenever a number ends in a “check digit” that was calculated from the digits before it, there is a good chance Luhn is behind it.

How the check works, step by step

The algorithm treats the last digit of the number as a check digit. A number is considered structurally valid if the whole sequence, including that check digit, satisfies the rule below.

  1. Start at the rightmost digit (the check digit) and move left.
  2. Leave the rightmost digit as it is, then double the value of every second digit as you continue to the left.
  3. If doubling produces a number greater than 9, subtract 9 from it. This is the same as adding the two digits of the result together.
  4. Add up all of the digits, both the doubled ones and the untouched ones.
  5. If the total is divisible by 10, the number passes. If not, it fails.

A worked example

Take the textbook example number 79927398713. It is only eleven digits long, so it is not a card number, but it is perfect for showing the arithmetic. Reading from the right, the digits are 3, 1, 7, 8, 9, 3, 7, 2, 9, 9, 7. We leave the first, double the second, leave the third, double the fourth, and so on.

  • Untouched digits: 3, 7, 9, 7, 9, 7. These add up to 42.
  • Doubled digits: 1 becomes 2, 8 becomes 16 and then 7, 3 becomes 6, 2 becomes 4, 9 becomes 18 and then 9. These add up to 28.
  • Total: 42 + 28 = 70.

Because 70 is divisible by 10, the number passes. If you change any single digit, the total moves away from a multiple of 10 and the check fails. That sensitivity is the whole point of the design.

What the Luhn check catches

The algorithm is tuned to the mistakes humans actually make. It detects:

  • Any single wrong digit. Typing a 5 instead of a 6 always changes the sum, so the check always fails.
  • Almost all swaps of two neighbouring digits. Typing 47 instead of 74 is caught, with one exception: the pair 09 swapped with 90 produces the same total.

It can miss a few patterns, including the twin errors 22 ↔ 55, 33 ↔ 66 and 44 ↔ 77. For a one-line check, however, its coverage of real-world typos is excellent. That is why it has survived for roughly seventy years.

What the Luhn check cannot tell you

Passing the Luhn check says only that the digits are internally consistent. It does not mean that:

  • the number was ever issued by a bank,
  • an account exists or is open,
  • a card has funds, is unexpired, or has not been reported lost, or
  • the person using the number is its rightful owner.

One in ten random digit strings passes the check by chance, so Luhn should never be treated as proof of anything. It is a filter for typos. Real confirmation always comes from the issuing bank during an authorization. Tvalidora follows the same principle: our card format checker reports whether a number is structurally sound and which network its prefix belongs to, and nothing more. Our FAQ explains in plain language what a “valid” result does and does not mean.

Implementing it in JavaScript

The algorithm is short enough to write in a few lines. The version below accepts a string, ignores spaces and hyphens, and returns true or false. Work with strings rather than numbers: card numbers can be 19 digits long, which is beyond the point where JavaScript numbers lose precision, and numbers can have leading zeros that must be preserved.

function passesLuhn(input) {
  const digits = String(input).replace(/[\s-]/g, "");
  if (!/^\d{12,19}$/.test(digits)) return false;

  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 sum % 10 === 0;
}

If you want to read more about the language features used here, the MDN JavaScript documentation is an excellent, free reference.

Practical tips for developers

  • Normalize before you check. Strip spaces and hyphens so that people can paste numbers in whatever grouping their card uses.
  • Remember the exceptions. Some card networks, such as UnionPay, issue numbers that do not always satisfy the Luhn rule, so a failed check on those should be reported gently rather than treated as a hard error.
  • Never rely on it alone. Use it to save the customer a failed attempt, then let your payment provider do the real verification.

Key takeaways

The Luhn algorithm is a small, elegant answer to a very practical problem: people make mistakes when they type long numbers. It catches every single-digit error and nearly every neighbouring swap, it needs no database, and it runs instantly in a browser. It is also strictly a format check, so use it for what it is good at and leave the question of whether a number is real to the institutions that issue it. To see the check in action, try the Tvalidora validator, or read our related guide on BIN numbers and issuer prefixes.

Put it into practice: check the format of a card, routing or account number with the Tvalidora validator, look up an area code or zip code with the US Checker, or read the FAQ to see exactly what a “valid” result means. Tvalidora checks number format only, and never stores what you enter.

Keep reading