Choose a card type
How many?
1

Test data only. Local fixtures do not issue cards, verify accounts or provide payment authorization. Use them only in authorized tests. Why?

VisaTEST CARD
•••• •••• •••• ••••
Cardholder—
ExpiresMM/YY
CVV•••
  • 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 sessionExpected behavior
Enter 378282246310005 with CID 0007Show 3782 822463 10005; accept four code digits for the local length check
Replace the number with a 16-digit Visa, leaving 0007 untouchedSwitch the brand and help text; flag the existing four-digit code instead of truncating it
User explicitly replaces 0007 with 007Accept the three-digit Visa code length; preserve both zeros
Replace Visa with Amex while retaining 007Ask for a four-digit CID; do not silently prepend a zero
Paste a 16-digit value beginning with 37Report a number-length error; do not slice it down to a passing 15-digit input
Delete back to a single 3Treat 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:

  1. Input value: type and paste 0007; inspect whether all four characters survive. An old maxlength="3" can discard a character before your application sees it.
  2. 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.
  3. 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.
  4. 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.
  5. 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.

Frequently Asked Questions

What should happen to an existing CID when the user switches to Visa?
Revalidate it against the new brand. The sample preserves the four-digit value and marks its length invalid for the 16-digit Visa comparison case, allowing the user to correct it rather than silently truncating it.
Why does the fixture use 0007?
It proves that a four-digit security-code string survives unchanged. Numeric conversion would turn it into 7 and destroy information before validation.
Does the example formatter handle a live input cursor?
No. It formats complete numbers and preserves overlong input for an error state. Test cursor position, paste and backspace separately in your actual input component.
Where should the form tell an Amex user to find the CID?
For the four-digit CID in this profile, the help text should point to the front of the card. Change the help text with the detected brand instead of hard-coding three digits on the back.