The last digit of a card number isn't random — it's a checksum designed by an IBM scientist in the 1950s specifically to catch the kind of typo a person makes when copying digits by hand.
How the Luhn algorithm works
The algorithm walks the digits from right to left, doubles every second digit, and subtracts 9 from any result over 9. If the number was entered correctly, the sum of all the resulting digits is divisible by 10 with no remainder.
Hans Peter Luhn: an IBM scientist, not a bank
The algorithm is named after Hans Peter Luhn, an IBM researcher who patented it in 1954 as a general-purpose way to catch transcription errors in any string of digits — not specifically for payment cards. It was later adopted by Visa, Mastercard, and virtually every other card network because it's cheap to compute and catches the two most common manual-entry mistakes: a single mistyped digit and two adjacent digits swapped.
Why a valid Luhn check doesn't mean the card is real
Luhn validation only confirms the number's mathematical structure, not that a real account is behind it. It's trivial to generate millions of numbers that pass the check — none of them tied to an actual card — which is why every real transaction still has to go through the issuing bank for the check that actually matters.
What this is useful for
- Catch a mistyped card number on the client before submitting a form.
- Quickly rule out an obvious typo when entering a long number by hand.
- Understand exactly what Luhn validation does and doesn't prove.
Identifying the card network from the number (BIN)
The first several digits of a card number are its BIN (Bank Identification Number), which identifies the network before any Luhn check even runs. Visa numbers start with 4, Mastercard with 51–55 or the newer 2221–2720 range, American Express with 34 or 37, and Discover with 6011 or 65. That's how a checkout form can show the right card-network icon while you're still typing, well before the full number is complete.