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 caret in place when a Diners number is edited
A formatter that displays 3612 345678 9013 correctly can still fail when the user edits
the middle. If it always moves the caret to the end, replacing one digit becomes difficult;
if it slices input at fourteen digits, an accidental extra digit disappears without an error.
This page tests both failures with a small, downloadable post-edit model.
The tool generates 14-digit, Luhn-valid numbers using 300–305, 36, 38 and 39,
with 4-6-4 display grouping and a three-digit synthetic security code. There is no length
selector. The example fixture above is synthetic and is not a gateway approval trigger.
Set the formatter’s contract
Cybersource documents a 14-digit Diners profile. Braintree’s configuration uses gaps after digits four and ten and also lists longer lengths. Our exercise is specifically the generator’s 14-digit profile, not a rule to reject every longer product.
The formatter takes the text after an edit and a collapsed caret offset, both from the same input state. It removes ordinary spaces and hyphens, groups the digits, and places the caret after the same number of digits. At a group boundary it chooses the position to the left of the inserted space. It does not decide the brand or check Luhn.
| Post-edit input | Expected display or result |
|---|---|
3612, caret at 4 | 3612, caret at 4; no trailing space |
36123, caret at 5 | 3612 3, caret at 6 |
36123456789013, caret at 14 | 3612 345678 9013, caret at 16 |
| Same unformatted input, caret at 7 | Same grouped display, caret at 8 rather than the end |
3612-345678-9013 | Same digits and 4-6-4 grouping after paste |
| A fifteenth digit or an embedded letter | Original text retained with an explicit error |
Caret offsets count displayed characters, including spaces. Number length counts digits.
Confusing those two measurements can cause a maxlength restriction to discard digits
before your formatter receives them.
Run the paste and caret fixtures
Save the post-edit input fixtures alongside the Diners caret checks :
node --test diners-caret.test.mjs
The cases include deleting and reinserting a middle digit, deleting a separator, pasting a complete number and retaining malformed input. Additional assertions check every caret position in the full unformatted fixture and ensure formatting the result again is stable.
function formatDinersEdit(value, caret) {
if (typeof value !== 'string' || !Number.isInteger(caret) ||
caret < 0 || caret > value.length) throw new TypeError('Invalid input selection');
if (!/^[0-9 -]*$/.test(value)) return { display: value, caret, error: 'characters' };
const digits = value.replace(/[ -]/g, '');
if (digits.length > 14) return { display: value, caret, error: 'too-long' };
const display = [digits.slice(0, 4), digits.slice(4, 10), digits.slice(10)]
.filter(Boolean).join(' ');
const before = value.slice(0, caret).replace(/[ -]/g, '').length;
// Keep the same number of digits before the caret, on the left of a separator.
let position = 0;
let count = 0;
while (count < before) {
if (display[position] !== ' ') count += 1;
position += 1;
}
return { display, caret: position, error: null };
}Connect your own formatter through an adapter returning display, caret and error to
reuse the expected objects. Do not copy this pure model into an event handler and assume
selection management is finished: it intentionally has no DOM dependency.
The separator-backspace trap still needs a browser test
If Backspace removes only a generated space, a formatter may immediately put that space back. The fixture models the resulting caret position, but cannot prove the interaction feels correct. In your input component, test Backspace and Delete on both sides of each separator, then replace a selected range crossing a separator. Decide whether those keys move across the space or remove the adjacent digit, and assert that behavior explicitly.
Also test keyboard navigation, a paste over selected text, touch selection and composition events where applicable. Avoid rewriting the value while an input method is composing text. Use a screen reader to confirm that a length error is available without moving focus away.
For a change of brand and security-code length, the Amex transition fixtures exercise a separate state problem. For raw-value normalization, compare the Visa input tests. The payment-form checklist places those checks alongside the full form flow.
A correctly preserved fourteen-digit string is still only input data. Use the provider sandbox cards for authorizations and declines; use the card validator when you need a separate checksum result.