The Luhn algorithm (mod-10) is the checksum that guards almost every payment card number, as well as IMEI device IDs and many national identifiers. It catches the most common data-entry mistakes — a single wrong digit or two swapped digits — before a number ever reaches a server. This tool runs that check locally and explains the result.
How it works
Working from the rightmost digit leftward:
- Double every second digit.
- If a doubled value is greater than 9, subtract 9 (equivalently, add its two digits).
- Add up all the resulting values.
- The number is valid when that total is a multiple of 10.
The final digit of the number is the check digit — it is specifically chosen so the whole sequence passes step 4. When a number fails, the tool computes what the check digit should have been, which usually pinpoints the typo.
It also inspects the leading digits to label the likely card brand (Visa starts with 4, Mastercard with 51-55 or 2221-2720, American Express with 34 or 37, and so on).
Tips and notes
- Spaces and dashes are ignored, so you can paste
4539 1488 0343 6467directly. - For example,
79927398713is a classic Luhn-valid test value — its digits sum to a multiple of 10. - Use the generator to create fictitious, Luhn-valid numbers per brand for testing checkout forms. These are not real cards and cannot be charged; they only exercise your validation logic.
- A pass means well-formed, not authorised. Always rely on your payment processor for real authorisation. Everything here stays on your device.
Card brand detection explained
The tool inspects the first one to four digits to identify the likely card network. The rules are publicly documented by each network:
| Brand | Prefix pattern |
|---|---|
| Visa | Starts with 4 |
| Mastercard | Starts with 51–55 or 2221–2720 |
| American Express | Starts with 34 or 37 |
| Discover | Starts with 6011, 622126–622925, 644–649, or 65 |
| Diners Club | Starts with 300–305, 36, or 38 |
The prefix check is a heuristic — it identifies the likely brand, not the issuing bank. A number starting with 4 that passes Luhn is Visa-format, but whether it maps to a real Visa account is a different question entirely.
How the check digit pinpoints a typo
When a number fails Luhn, the expected check digit tells you what the last digit should have been. If you typed a number and the last digit is wrong, the expected-vs-actual comparison reveals the discrepancy instantly. More usefully, if the number is long (16 or 19 digits) and the check digit seems correct, the actual typo is likely in one of the other positions — perhaps a transposition of two adjacent digits in the middle. In that case, systematically swapping adjacent pairs and re-checking is faster than re-entering the whole number.
Why Luhn is not an anti-fraud measure
The Luhn algorithm was designed in 1954 to detect accidental data-entry errors before sending data over slow and expensive networks. It was never intended to stop deliberate fraud. Any number of Luhn-valid cards can be generated by anyone with this tool, a calculator, or five minutes of arithmetic. The actual fraud prevention in modern card payments is layered on top: CVV2 verification, address verification (AVS), 3D Secure authentication, EMV chip cryptograms, and real-time network fraud scoring. Luhn is the first, simplest gate — it rules out obvious typos, nothing more.
Common developer mistakes with Luhn
- Stripping only spaces but not dashes: card numbers are sometimes formatted with dashes; strip both before checking
- Checking Luhn on test transactions against a sandbox that ignores Luhn: the test environment may accept any number, making Luhn failures invisible until production
- Assuming Luhn failure means the user typed wrong: a failed Luhn on a physically issued card more often means a digit was mis-scanned by the camera or misread from a worn emboss — prompt the user to try again before showing an error