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

  1. Write down the payload without a check digit. Keep it as a string of ASCII digits.
  2. Double every second digit, starting from the rightmost payload digit. Alternate doubling as you work toward the left.
  3. Subtract 9 from any doubled value above 9. For example, 16 contributes 7.
  4. 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.
Worked example Luhn check digit for a 15-digit test payload
Payload453914880343657?
1Double×2×2×2×2×2×2×2×2
Result856924168038312514
2Over 9 → −9856924780383355
  1. 3Sum every digit8+5+6+9+2+4+7+8+0+3+8+3+3+5+5 = 76
  2. 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

InputChecksum helperInterpretation
4539148803436574TrueThe worked example’s arithmetic is consistent
4111111111111112FalseA single-digit change from the passing fixture is detected
Empty input or abcFalseNo valid input to check
4111x111111111111FalseThe helper rejects the letter; it does not delete it
0FalseOutside this helper’s minimum two-digit contract
00TrueArithmetic 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.

Frequently Asked Questions

Does Luhn prove a number belongs to an account?
No. It is arithmetic over a digit string. A generated number can satisfy the same calculation without being issued or authorized for payment.
Why does the example helper reject a single zero?
A single zero has a sum divisible by ten, but this helper’s input contract requires at least two digits. That contract is separate from the much longer card-number profiles used by the site tools.
Does Luhn detect every typing error?
No. It detects every single-digit substitution, but swapping adjacent 09 and 90 can leave the checksum unchanged. Multiple changes can also cancel out.
Should a checksum failure always block or always allow submission?
Neither follows from this calculation alone. Keep the checksum result separate from your configured form policy and follow the requirements of your payment provider.