Test Credit Card Numbers: How the Luhn Check and Card Prefixes Work
Payment forms validate card numbers before a single request reaches your gateway. To test that validation you need numbers that look exactly right — correct prefix, correct length, valid checksum — but can never be charged. Here’s how they work.
Anatomy of a card number
- IIN / BIN (first 6–8 digits): identifies the network and issuer — 4 for Visa, 51–55 and 2221–2720 for Mastercard, 34/37 for Amex.
- Length: 16 digits for most networks, 15 for Amex, 14 for Diners Club.
- Check digit: the final digit, calculated with the Luhn algorithm.
The Luhn algorithm, step by step
- Starting from the right, double every second digit.
- If doubling gives more than 9, subtract 9.
- Add all digits together.
- If the total is divisible by 10, the number is valid.
Luhn catches most single-digit typos and swapped neighbours — that’s why every checkout uses it.
Generated numbers vs gateway test cards
Generated numbers are perfect for front-end checks: card-type detection, formatting, Luhn validation and UI. For end-to-end tests against Stripe, Braintree, Adyen or PayPal sandboxes, use the official test cards those providers publish — they trigger specific approvals, declines and 3-D Secure flows.
Generated numbers are not linked to any account and will be declined by real processors. Using them to attempt purchases is fraud.
Pro tips
- Test spaces and dashes in input — users type them.
- Check that Amex shows a 4-digit CVV field.
- Verify that the card logo switches as the user types the prefix.