Oct
9

How Credit Card Validation Works: The Luhn Algorithm

10/09/2026 02:28 PM oleh Seovaro Team dalam


How Credit Card Validation Works: The Luhn Algorithm

Type a credit card number into any checkout form and something checks it before the page even talks to the bank. That instant "invalid card number" message — appearing the moment you finish typing — comes from a checksum formula invented in 1954 by IBM scientist Hans Peter Luhn. It catches typos, transposed digits, and made-up numbers with remarkable efficiency, all without knowing anything about your account. Want to see it in action? Run any number through our free credit card validator.

This guide explains what happens when you type a card number, walks through the Luhn algorithm step by step with a worked example, shows how the first digits identify the card brand (Visa, Mastercard, Amex, Discover), and clarifies what validation proves — and what it does not.

What Happens When You Type a Card Number

A checkout form validates your card number in layers, fastest first:

  1. Format check. Are there only digits (plus spaces/dashes)? Is the length plausible — 13 to 19 digits depending on the brand?
  2. Brand detection. The first few digits (the IIN, see below) identify Visa, Mastercard, and friends. The form may show the brand logo as you type.
  3. Luhn checksum. The full number must satisfy the Luhn formula. This is the mathematical gatekeeper — it rejects the vast majority of random or mistyped numbers instantly, client-side, with no network request.
  4. Bank authorization. Only after the above pass does the number go to the payment processor, which checks that the account exists, has funds, and is not stolen. Luhn knows nothing about any of this.

Steps 1–3 happen in milliseconds in your browser. They exist to catch user error early, not to detect fraud — a subtle but important distinction we will return to.

The Luhn Algorithm, Step by Step

The algorithm is beautifully simple. To validate a number:

  1. Starting from the rightmost digit (the check digit) and moving left, double every second digit.
  2. If doubling produces a number greater than 9, subtract 9 (equivalently, sum its digits).
  3. Sum all the digits.
  4. If the total is a multiple of 10, the number is valid.

Worked example with 4539 1488 0343 6467 (a test Visa number):

Original:     4 5 3 9 1 4 8 8 0 3 4 3 6 4 6 7
Double every
2nd from right: 8   6   2   8   0   8   12  12
(12 > 9, so subtract 9 → 3 and 3)

Adjusted:     8 5 3 9 2 4 3 8 0 3 8 3 3 4 3 7
Sum: 8+5+3+9+2+4+3+8+0+3+8+3+3+4+3+7 = 80
80 mod 10 = 0  →  VALID ✓

Now watch what happens with a single typo — change the last digit from 7 to 8:

Sum becomes 81 → 81 mod 10 = 1  →  INVALID

One wrong digit breaks the checksum. The algorithm catches 100% of single-digit errors and nearly all transpositions of adjacent digits (the two most common typing mistakes), which is exactly what it was designed for — it was invented to validate punched-card serial numbers, where miskeying was the enemy.

How Card Brands Are Detected: IIN Ranges

The first 6–8 digits of a card number are the Issuer Identification Number (IIN, formerly BIN). They identify the network and often the issuing institution:

  • Visa — starts with 4. Length 13, 16, or 19 digits.
  • Mastercard — starts with 51–55 or 2221–2720 (the newer 2-series range). Always 16 digits.
  • American Express — starts with 34 or 37. Always 15 digits, with a 4-6-5 grouping instead of 4-4-4-4.
  • Discover — starts with 6011, 622126–622925, 644–649, or 65. 16 or 19 digits.
  • Diners Club — starts with 300–305, 36, or 38. 14 digits.
  • JCB — starts with 35. 16 to 19 digits.

This is why checkout forms can display the Visa logo after you type a single "4" — brand detection needs only the prefix. Combined with the expected length per brand, the form can also flag a 15-digit "Visa" as suspicious before Luhn even runs. Try it yourself with our credit card validator, which reports both the brand and the checksum result.

The Check Digit: How Valid Numbers Are Made

The last digit is not random — it is computed to make the whole number pass Luhn. To generate it: run the algorithm on the number without the final digit (doubling starts from what will be the second-to-last position), sum the results, and choose the check digit that brings the total to the next multiple of 10.

This is how test card numbers from payment processors (Stripe, PayPal, and others publish them) are constructed, and how "valid-looking" fake numbers in movies are generated. Anyone can produce a Luhn-valid number in seconds — which brings us to the critical limitation.

What Validation Does NOT Prove

This is the part people misunderstand most. A number passing Luhn validation means exactly one thing: the digits are internally consistent. It does not mean:

  • The card exists. Most Luhn-valid numbers were never issued to anyone.
  • The card is active or has funds. Only the issuing bank knows that, via authorization.
  • The card belongs to the person typing it. A stolen card number passes Luhn just as happily as a legitimate one. Fraud detection is a completely separate system (CVV, AVS, 3-D Secure, behavioral analysis).
  • The number is "real" in any meaningful sense. Luhn-valid numbers are trivially generatable, which is why developers must never treat client-side validation as a security boundary.

For developers, the takeaway: Luhn validation is UX, not security. It belongs in the form to give instant feedback on typos. Real authorization always happens server-side through a payment processor, and card data must never touch your own servers unless you are PCI DSS compliant — which is why tokenizing processors like Stripe exist.

Final Thoughts

The Luhn algorithm is a 70-year-old piece of elegant engineering: double every second digit from the right, subtract 9 over 9, sum, check for a multiple of 10. It catches virtually every typo at zero cost, and the IIN prefix system layers brand detection on top. Just remember its boundary — it proves a number is well-formed, nothing more. The bank always has the final word.

Curious about a number? Test the checksum and brand detection instantly with our free credit card validator.

FAQ

What is the Luhn algorithm?

A checksum formula that validates identification numbers. Starting from the rightmost digit, double every second digit (subtracting 9 from results over 9), sum all digits, and the number is valid if the total is a multiple of 10. It catches all single-digit errors and most transpositions.

Can a fake credit card number pass Luhn validation?

Yes, easily. Luhn only checks internal consistency of the digits, not whether the card exists or is active. Valid-looking test numbers are generated this way deliberately. Only bank authorization confirms a real card.

How do websites detect my card brand as I type?

The first 6–8 digits (the Issuer Identification Number) identify the network: 4 for Visa, 51–55 or 2221–2720 for Mastercard, 34/37 for Amex, and so on. The prefix alone is enough to show the brand logo.

Is Luhn validation a security feature?

No — it is a user-experience feature. It gives instant feedback on typos before the form submits. Real security comes from CVV checks, address verification, 3-D Secure, and server-side authorization through a PCI-compliant payment processor.