An Amex form can display the right logo while holding an incomplete PAN, or show 15 digits while the check digit is wrong. Treating all of those states as “not Amex” creates misleading errors. This guide uses a concrete failing value to separate detection, completion and checksum validation.
Diagnose a failure without changing its brand
The local profile has prefixes 34 and 37, a 15-digit PAN and a four-digit security code. The Amex generator supplies fixtures for that profile; the card validator reports each layer independently.
348282246310005 starts with 34 and has 15 digits. It is an Amex format candidate with a failed checksum, not a number from another brand. Its payload would need check digit 2. Changing a fixture’s last digit can repair the checksum for a local test; it cannot repair or discover a real account.
| Case | Input / condition | Expected result |
|---|---|---|
| amex-checksum | '348282246310005' | {"brand":"amex","checksum":"fail","length":"supported","status":"ok"} |
| amex-valid | '378282246310005' | {"brand":"amex","checksum":"pass","length":"supported","status":"ok"} |
| amex-incomplete | '37828224631000' | {"brand":"amex","checksum":"fail","length":"incomplete","status":"ok"} |
| amex-letter | '3782x82246310005' | {"brand":null,"checksum":"not-checked","length":"unresolved","status":"characters"} |
| amex-group | 15-digit display | "3782 822463 10005" |
The shortened example has 14 digits. A checksum can be computed within the tool’s general length envelope, but the Amex length still remains incomplete. Do not promote a checksum result into a complete-card decision.
Format the display after validating characters
The standard display in this project is 4-6-5: 3782 822463 10005. The shared preview function also handles other configured brands and refuses malformed characters:
export function previewNumber(input, reveal, profile) {
const normalized = normalizeInput(input);
if (normalized.status === 'empty') return '•••• •••• •••• ••••';
if (normalized.status !== 'ok' || normalized.digits.length > 19) throw new TypeError('Use up to 19 digits with spaces or hyphens; other characters are not removed');
const { digits } = normalized;
const brand = prefixCandidates(digits, profile).brand;
const groups = brand === 'amex' ? [4, 6, 5] : brand === 'diners' && digits.length === 14 ? [4, 6, 4] : [4, 4, 4, 4, 3];
const displayed = reveal === 'full' ? digits : '•'.repeat(Math.max(0, digits.length - 4)) + digits.slice(-4);
let offset = 0;
return groups.map(size => { const part = displayed.slice(offset, offset + size); offset += size; return part; }).filter(Boolean).join(' ');
}Here, reveal: 'full' is for synthetic fixture display. The other branch masks all but the last four digits. A masked rendering does not remove the original input from memory or storage. The PAN anatomy guide explains the separate storage question.
For an editable field, also test insertion, selection replacement, deletion and caret position. The pure formatter above returns text; it does not implement those browser interactions. Use the brand detection guide when a partial prefix has several candidates, rather than switching every number beginning with 3 to Amex.
Change the code field without silently changing its value
When a user moves from a three-digit-code network to Amex, 003 becomes too short. When moving back, 0007 becomes too long. Preserve those strings and show a format error; do not pad the first or truncate the second. The security-code generator provides synthetic values with explicit lengths.
| Input | Expected length | Local format result |
|---|---|---|
003 | 3 | Pass; zeros remain |
003 | 4 | Fail; no automatic padding |
0007 | 4 | Pass; zeros remain |
0007 | 3 | Fail; no automatic truncation |
These transitions are asserted in the downloadable tests. Length is only a local input rule. The CVV explanation separates it from provider verification and payment approval.
Run the diagnostic project
Extract the shared guide examples into one directory. With Node.js 22 or later:
node --test guide-examples.test.mjs
node guide-demo.mjs amex
The tests assert the mislabelled checksum case, strict paste handling, 4-6-5 output and leading-zero code values. The displayed case results come from the same fixture file, not a second table maintained by hand. To compare completion rules across networks, use the card-length reference.