Work from a payload to its check digit
Luhn adds a check digit so that a transformed sum is divisible by ten. The arithmetic is useful for detecting certain typing errors. It does not query a bank, verify a security code or establish that an account exists.
Use the synthetic payload 453914880343657. It has fifteen digits and is missing the
final check digit. The table below calculates each transformation from that payload.
Calculating the check digit
- Write down the payload without a check digit. Keep it as a string of ASCII digits.
- Double every second digit, starting from the rightmost payload digit. Alternate doubling as you work toward the left.
- Subtract 9 from any doubled value above 9. For example, 16 contributes 7.
- Take the sum modulo 10 and subtract it from 10. Apply modulo 10 once more so a sum already divisible by ten produces check digit zero.
| Payload | 4 | 5 | 3 | 9 | 1 | 4 | 8 | 8 | 0 | 3 | 4 | 3 | 6 | 5 | 7 | ? |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1Double | ×2 | ×2 | ×2 | ×2 | ×2 | ×2 | ×2 | ×2 | ||||||||
| Result | 8 | 5 | 6 | 9 | 2 | 4 | 16 | 8 | 0 | 3 | 8 | 3 | 12 | 5 | 14 | |
| 2Over 9 → −9 | 8 | 5 | 6 | 9 | 2 | 4 | 7 | 8 | 0 | 3 | 8 | 3 | 3 | 5 | 5 |
- 3Sum every digit
8+5+6+9+2+4+7+8+0+3+8+3+3+5+5 = 76 - 4Check digit
(10 − (76 mod 10)) mod 10 = (10 − 6) mod 10 = 4
Result 4539 1488 0343 6574 ✓ Passes Luhn
The transformed payload sum is 76, so the check digit is 4. Appending it produces
4539148803436574. The complete-number calculation now sums to 80.
Verifying a complete number
When the check digit is already present, start with the rightmost digit undoubled. Then alternate doubling to the left, subtract nine where needed, and require the total to be divisible by ten. Generating a digit starts with doubling; validating a complete number starts without it. Confusing those starting positions is a parity error.
Separate the arithmetic from input cleanup
The site uses this shared implementation. normalizeInput accepts only ASCII digits,
ordinary/nonbreaking spaces and hyphens; unsupported characters and numeric values do not
become digit strings silently. luhnCheckDigit takes a nonempty, digits-only payload.
function luhnSum(digits, double) {
let sum = 0;
for (let i = digits.length - 1; i >= 0; i -= 1, double = !double) {
let digit = digits.charCodeAt(i) - 48;
if (double) digit = digit * 2 > 9 ? digit * 2 - 9 : digit * 2;
sum += digit;
}
return sum;
}
export function luhnValid(input) {
const { status, digits } = normalizeInput(input);
// This helper's input contract requires at least two digits, not a PAN length.
return status === 'ok' && digits.length >= 2 && luhnSum(digits, false) % 10 === 0;
}
export function luhnCheckDigit(payload) {
if (typeof payload !== 'string' || !payload || /[^0-9]/.test(payload)) return null;
return String((10 - luhnSum(payload, true) % 10) % 10);
}The complete module, including normalizeInput, is in
the downloadable validation bundle
.
The four-language code guide shows the same input
contract in JavaScript, Python, PHP and Ruby and explains how to run the shared vectors.
A passing checksum is only one result
| Input | Checksum helper | Interpretation |
|---|---|---|
4539148803436574 | True | The worked example’s arithmetic is consistent |
4111111111111112 | False | A single-digit change from the passing fixture is detected |
Empty input or abc | False | No valid input to check |
4111x111111111111 | False | The helper rejects the letter; it does not delete it |
0 | False | Outside this helper’s minimum two-digit contract |
00 | True | Arithmetic passes; this is not a complete card number |
The card validator reports prefix and length separately. The number-structure guide explains those other fields. Do not turn a checksum boolean into a statement that a card is active, funded or accepted.
Test what the checksum can miss
For equal-length ASCII digit strings, changing any one digit changes its contribution to the checksum. The downloadable JavaScript tests exercise every single-digit substitution of the fixed passing PAN examples.
Adjacent 09 and 90 are a useful counterexample to stronger claims: either ordering
can contribute the same total under alternating doubling. Multiple mistakes can also cancel
out. A passing result therefore does not prove that no transcription error occurred.
The public arithmetic and its original purpose can be inspected in
Luhn’s patent.
Extract the bundle and run node --test luhn.test.mjs. It checks fixed expected digits,
normalization failures and round trips across payload lengths. You can create additional
synthetic inputs with the prefix generator, keeping expected results
independent from the implementation under test.
Decide what the form does separately
A checksum failure is not a universal instruction to submit or to block. Validate according
to your integration’s declared contract. The
UnionPay policy exercise demonstrates how the checksum can
remain fail while an explicitly configured local decision changes. This distinction is
also useful when checking a broader payment-form test plan.