What Luhn can and cannot check
Luhn evaluates only the digits in a sequence. It catches many single-digit mistakes and some adjacent transpositions without contacting a network, issuer, or payment provider.
A pass therefore means only that the checksum is consistent. It says nothing about allocation, account status, available funds, or the result of a sandbox or live authorization.
NEXT STEPContinue with actionable error reasons for validation results
How the check digit is calculated
Starting immediately left of the check digit, double every second digit. Subtract 9 from results above 9, add every processed and untouched digit, then choose a check digit that makes the total divisible by 10.
Treat the number as a string in code. Converting a long card-like value to a JavaScript Number can lose precision and removes meaningful leading zeroes.
- Remove display-only spaces and hyphens
- Reject every other unsupported character
- Check global and network-specific length first
- Run Luhn last and return a specific reason
A useful QA test matrix
Do not stop at one passing number. Cover valid length plus valid Luhn, valid length plus broken Luhn, valid Luhn plus wrong network length, and input containing unsupported characters.
Each case should fail for one reason so UI messages, masking, logging, and backend error mappings remain easy to diagnose. TestPAN Studio's negative modes isolate those variables.
Move from local checks to a payment sandbox
Local Luhn cases are ideal for constraints, formatting, masking, and validation states. Move to official provider sandbox cards for 3DS, decline reasons, risk controls, and authorization outcomes.
Never submit generated values to a live payment environment, and keep complete values out of analytics, crash reports, and server logs.