Test data only. Local fixtures do not issue cards, verify accounts or provide payment authorization. Use them only in authorized tests. Why?
5 test cards
- Generated fields are not uploaded
- Randomly generated in your browser
- Every number passes the Luhn check
Keep the form correct when the customer changes card type
The difficult Amex defect is often a transition: a form works when loaded with an Amex
card, then truncates its CID after the user replaces a Visa number. Test both directions
without reloading the page. This generator starts with 15-digit Amex numbers, 4-6-5
grouping and a four-digit synthetic security code; selecting Visa gives you a comparison
record for the same session.
The Amex format guide covers the numbering rules. This page adds a repeatable field-state exercise, including what happens to a security code already typed into the form.
American Express card number format
Use 34 and 37 as Amex prefix candidates, 15 digits for the complete number, and four
digits for the CID in this test profile.
Cybersource documents the number shape;
American Express identifies the four-digit code on the front of the card.
Neither matching the shape nor having four digits verifies a security code.
The transition matrix
Keep the security-code input as text, including leading zeros. The examples use 0007 and
007 because a numeric conversion would destroy the distinction before your length check.
| Action in the same form session | Expected behavior |
|---|---|
Enter 378282246310005 with CID 0007 | Show 3782 822463 10005; accept four code digits for the local length check |
Replace the number with a 16-digit Visa, leaving 0007 untouched | Switch the brand and help text; flag the existing four-digit code instead of truncating it |
User explicitly replaces 0007 with 007 | Accept the three-digit Visa code length; preserve both zeros |
Replace Visa with Amex while retaining 007 | Ask for a four-digit CID; do not silently prepend a zero |
Paste a 16-digit value beginning with 37 | Report a number-length error; do not slice it down to a passing 15-digit input |
Delete back to a single 3 | Treat the brand as unresolved; do not retain Visa’s or Amex’s final validation state |
These expectations are for a local form model. They do not claim that any of the synthetic
security codes match an issuer’s records. The number 378282246310005 is also listed in
Stripe’s test documentation; its sandbox behavior is a
separate test from the field transitions here.
Run the form-state checks
Save the transition fixtures and the runnable Amex checks together, then run:
node --test amex-transitions.test.mjs
The download executes seven fixture cases, a complete Amex → Visa → Amex transition, and a numeric-code rejection. Below is the core model used by those tests; the complete file contains its imports and assertions.
function describeFields(number, securityCode) {
const brand = /^3[47]/.test(number) ? 'amex' : /^4/.test(number) ? 'visa' : 'unknown';
const numberShapeValid = brand === 'amex' ? /^3[47]\d{13}$/.test(number) :
brand === 'visa' && /^4\d{15}$/.test(number); // 16-digit Visa comparison case
const codeLength = brand === 'amex' ? 4 : brand === 'visa' ? 3 : 0;
const codeLengthValid = typeof securityCode === 'string' &&
/^\d+$/.test(securityCode) && securityCode.length === codeLength;
let display = number;
if (numberShapeValid) {
display = brand === 'amex' ?
[number.slice(0, 4), number.slice(4, 10), number.slice(10)].join(' ') :
number.match(/.{1,4}/g).join(' ');
}
// Retain overlong input and show an error; never silently drop a digit.
return { brand, display, numberShapeValid, codeLengthValid, securityCode };
}The Visa branch deliberately handles only the 16-digit comparison record. It is not a
complete multi-network validator. To exercise your checkout, replace describeFields with
an adapter that reads the state produced by your own form and keep the expected objects.
If you use hosted payment fields, use your provider’s test interface instead of trying to
read inside its iframe.
What to inspect in the browser
A unit test cannot prove that the DOM input preserves a fourth digit. Run the transition matrix in your development checkout and check these separate pieces of UI:
- Input value: type and paste
0007; inspect whether all four characters survive. An oldmaxlength="3"can discard a character before your application sees it. - Label and help text: when Amex is selected, show four digits on the front rather than a fixed “three digits on the back” instruction. Restore the other brand’s guidance when the customer switches back.
- Error recovery: retaining an overlong value and explaining the required length lets the user correct it. Silently changing that value can hide the original defect.
- Focus and accessibility: a brand update should not move focus away from the active field. Make the revised field error available to a screen reader as well as visually.
- Submitted test value: verify that validation and serialization use the same unformatted number and unchanged security-code string.
For grouping-only failures, compare a complete 15-digit input with the 4-6-5 display;
check insertion and backspace in the middle of the value as well as pasting it all at once.
The formatter in this sample only formats complete inputs, so it does not implement cursor
management for a live input mask.
Where to take the failing test next
Use the CVV generator for additional synthetic code values, and the Visa input tests when the failure is caused by normalization or storage precision. A brand match is not a checksum result; the card validator checks that separately.
For gateway approval, declines or authentication challenges, use the provider sandbox reference. Keep the form regression independent so a network response cannot hide a field-state failure.