Every credit card number carries a built-in error detector invented in the 1950s. It won't tell you a card is real — but it'll catch the typo before you ever hit the payment network.

Type a credit card number into a checkout with one digit wrong and, more often than not, the form flags it instantly — before anything talks to a bank. That's not magic and it's not a database lookup. It's a checksum baked into the number itself, designed in the 1950s, still doing its job on most card numbers in use today. Understanding it demystifies a small but everywhere-present piece of infrastructure, and it clears up a dangerous misconception about what "valid card number" actually means.

What the Luhn algorithm is

The formula has a name and a clear purpose. Wikipedia describes it:

"The Luhn algorithm or Luhn formula (creator: IBM scientist Hans Peter Luhn), also known as the "modulus 10" or "mod 10" algorithm, is a simple check digit formula used to validate a variety of identification numbers."

— Wikipedia, "Luhn algorithm" (CC BY-SA 4.0)

It's a check digit scheme: the last digit of the number is calculated from the others, so a computer can instantly test whether the whole thing is internally consistent. The same article notes that it is specified in ISO/IEC 7812-1 and is in the public domain, and lists users well beyond cards, including IMEI numbers on phones and SIM card ICCIDs. Luhn's US patent for a hand-held mechanical device that computed it was granted in 1960; the "1954" in this article's title refers to when the method was devised, per the same page.

The math, in plain steps

It's genuinely simple. Starting from the rightmost digit and moving left, you double every second digit. If doubling produces a two-digit result (say 8 → 16), you add those digits together (1 + 6 = 7) — or equivalently subtract 9. Then you sum every digit (the doubled-and-adjusted ones plus the untouched ones). If that total is divisible by 10, the number passes. The final digit of the card is chosen precisely so the sum comes out to a multiple of 10 — that's the "modulus 10" in the name. No cryptography, no lookup table, just doubling and adding.

A worked example, digit by digit

Take an invented 16-digit number, 4000 1234 5678 9017. It was built for this article; passing the check says nothing about whether any such account exists, which is the point of the section after next. Count positions from the right, starting at 1. Even positions get doubled:

Position from rightDigitDoubled?Value added
17no7
21yes, 22
30no0
49yes, 18 → 99
58no8
67yes, 14 → 55
76no6
85yes, 10 → 11
94no4
103yes, 66
112no2
121yes, 22
130no0
140yes, 00
150no0
164yes, 88

The column sums to 60, a multiple of 10, so the number passes. Now change the sixth digit from 2 to 3 (4000 1334 5678 9017). That digit is eleventh from the right, which is not a doubled position, so it counts at face value: its contribution rises from 2 to 3, the total becomes 61, and the check fails. To generate a check digit, you run the same procedure on the first 15 digits with a placeholder 0 at the end and pick the digit that brings the total up to the next multiple of 10.

Which errors it catches

The design target was human data-entry mistakes, and it's well matched to them. Luhn catches every single-digit error — get any one digit wrong and the checksum fails. Our script tried all 144 possible single-digit substitutions on the example number and every one failed the check. It also catches nearly all adjacent transpositions, the very common slip of swapping two neighbouring digits (typing 21 as 12). Over 26,994 random adjacent swaps on randomly generated valid numbers, 97.8% were caught — which matches the theory: of the 45 distinct pairs of different digits, exactly one pair slips through.

The errors it misses

No simple checksum catches everything, and knowing the gap matters. The one adjacent transposition Luhn cannot see is 09 ↔ 90: swapping those digits leaves the checksum unchanged. It also misses three "twin errors", where a repeated digit is mistyped as a different repeated digit: 22 ↔ 55, 33 ↔ 66 and 44 ↔ 77. Our brute-force run found exactly those and no others. This isn't a flaw so much as the price of simplicity: a check digit is a cheap filter, not a guarantee. Stronger schemes exist — Wikipedia names the Verhoeff and Damm algorithms — but they cost more to compute by hand, and card numbers kept Luhn.

There is also a statistical floor worth keeping in mind. Because the check only looks at the last digit of a sum, roughly one random number in ten passes it. Our script generated 200,000 random 16-digit strings and 10.1% passed. So Luhn is good at catching a slip on a real number, and useless as evidence that a string of digits was ever a real number at all.

"Passes Luhn" is not "is a real card"

Here's the misconception worth killing. Passing the Luhn check means only that the number is internally consistent — the digits add up correctly. It says nothing about whether the card exists, belongs to anyone, has funds, or hasn't expired. You can generate Luhn-valid numbers all day that correspond to no real account. So Luhn is the right tool for one job — catching typos on the client side before you bother the payment network — and completely the wrong tool for "is this a legitimate card?", which only the issuer, through the actual payment authorisation, can answer. Treat a Luhn pass as "worth submitting," never as "verified."

Implementation mistakes that bite

FAQ

Does Luhn tell me the card brand? No. The brand comes from the leading digits (the issuer prefix); Luhn only checks the last digit against the rest.

Is Luhn a security feature? No. Wikipedia is explicit that it "was designed to protect against accidental errors, not malicious attacks". Anyone can compute a valid check digit.

Should my form block submission on a Luhn failure? Showing an inline "check the number" message is reasonable. The real decision still comes from the payment authorisation.

Check it, and know its limits

Use it for exactly what it's good at. A Luhn check on the client side, with the network identified from the prefix, catches a mistyped number instantly — a cheap check that saves a doomed round-trip. To appreciate that Luhn is a checksum and not a security measure, contrast it with a real one-way function in a hash generator: a hash is built to resist tampering; Luhn is built only to catch honest slips. And when you're writing the input handling around a card field — stripping spaces, matching the digit pattern before you even run Luhn — a regex tester helps you get that pattern right. A 1954 checksum still guarding modern payments is a nice reminder that the best infrastructure is often the simplest.

← All articles