A card field for the number, expiry and security code. The number groups for its brand as it is typed with the caret kept, the brand shows as a neutral glyph, and each field checks itself and says what is wrong. Split into labelled fields, or one row where expiry and code fold in after a valid number.
On this page
- Add or update a card in billing settings
- The card step of a checkout
- A trial that asks for a card
- Pay a single invoice
Examples
Inline
Errors
American Express
Disabled
Installation
npx knowns-ui@latest add card-inputPaste this into your coding agent (Claude Code, Cursor, Codex). It checks your project, sets up the registry, runs the install and verifies it.
Install Card input from Knowns UI into this project. Work step by step, show me each change, and ask before you edit files outside this project.
Context: Knowns UI (https://ui.knowns.dev) is a shadcn registry. Items land in the project as source files. Docs: https://ui.knowns.dev/docs/card-input
1. Check the project first. Stop and tell me if any of these is missing:
- React 19 and Tailwind CSS v4.
- A components.json at the project root. If there is none, ask me before running `npx knowns-ui@latest init --no-key` (it runs shadcn init when needed, then adds the registries).
2. Run:
```bash
npx knowns-ui@latest add card-input
```
It writes these files:
- lib/knowns/run.ts
- lib/knowns/json.tsx
- lib/knowns/status.ts
- lib/knowns/tone.ts
- lib/knowns/types.ts
- lib/knowns/use-abortable.ts
- lib/knowns/utils.ts
- components/ui/knowns/kbd.tsx
- components/ui/knowns/field-class.ts
- components/ui/knowns/textarea.tsx
- components/ui/knowns/chat-composer/use-autosize.ts
- lib/knowns/motion.tsx
- lib/knowns/motion-utils.ts
- components/ui/knowns/use-floating.ts
- components/ui/knowns/tooltip.tsx
- components/ui/knowns/tooltip-utils.ts
- components/ui/knowns/input.tsx
- components/ui/knowns/card-input/card-input.tsx
- components/ui/knowns/card-input/card-brand-glyph.tsx
- components/ui/knowns/card-input/card-input-utils.ts
and installs these npm packages: clsx@^2.1.1, tailwind-merge@^3.7.0.
3. While it runs:
- It also adds a few @keyframes (all named kn-*) to the global CSS file (globals.css or the file components.json points at). Let it: they only add animations.
- If it asks to overwrite files under lib/knowns/: these are shared by every Knowns UI item. List them, say whether I changed them (check git), and ask me before you answer.
- If it asks to overwrite any other file, show me the diff and ask me.
4. Verify:
- Every file in the list above exists.
- The project type-checks (for example `npx tsc --noEmit`) and the dev build has no new errors.
5. Use it: follow the examples and props on https://ui.knowns.dev/docs/card-input, and ask me if anything about my data is unclear.
If the Knowns UI MCP server is connected (https://ui.knowns.dev/docs/ai-and-mcp), use get_component({ name: "card-input" }) or search_docs for details.
Finish with a short summary: the files you added or changed, the packages you installed, and anything I still need to do.Props
- value / defaultValue
{ number, expiry, cvc, name? }- The formatted text of each field ("4242 4242 4242 4242", "08 / 29"). Controlled with value, or uncontrolled from defaultValue.
- onChange
(value, details) => void- Every edit. details: { brand, digits, last4, expiry: { month, year } | null, complete, errors }.
- onComplete
(details) => void- Once, when an edit makes every field check out.
- onFieldBlur
(field) => void- A field lost focus: number, expiry, cvc or name.
- layout
"split"|"inline"- Default
"split" - Split: labelled fields, number then expiry and code. Inline: one row, expiry and code fold in after a valid number.
- size
"sm"|"md"|"lg"- Default
"md" - 28, 32 or 36px high, the same as Input; taller on touch screens.
- showErrors
boolean- Default
false - Show every current error now, e.g. after a submit. Otherwise a field shows its error once it is left with text in it.
- errors
{ number?, expiry?, cvc?, name? }- The errors to show in place of the field's own, e.g. from your form or a decline.
- autoAdvance
boolean- Default
true - Move to the expiry when the number is done, and to the code when the expiry is.
- askName
boolean- Default
false - Ask for the name on the card too.
- label / labels
ReactNode|null / { number?, expiry?, cvc?, name? }- Default
"Card" / - - The inline row's label (the split group's name for screen readers), and the field labels.
- disabled
boolean- Default
false - Every field off.
- now
number- Default
Date.now() - For the expiry check.
- ref
{ focus(field?) }- Focus a field, or the first one with an error. Opens the inline fold when needed.
| Prop | Type | Default | Description |
|---|---|---|---|
| value / defaultValue | { number, expiry, cvc, name? } | - | The formatted text of each field ("4242 4242 4242 4242", "08 / 29"). Controlled with value, or uncontrolled from defaultValue. |
| onChange | (value, details) => void | - | Every edit. details: { brand, digits, last4, expiry: { month, year } | null, complete, errors }. |
| onComplete | (details) => void | - | Once, when an edit makes every field check out. |
| onFieldBlur | (field) => void | - | A field lost focus: number, expiry, cvc or name. |
| layout | "split" | "inline" | "split" | Split: labelled fields, number then expiry and code. Inline: one row, expiry and code fold in after a valid number. |
| size | "sm" | "md" | "lg" | "md" | 28, 32 or 36px high, the same as Input; taller on touch screens. |
| showErrors | boolean | false | Show every current error now, e.g. after a submit. Otherwise a field shows its error once it is left with text in it. |
| errors | { number?, expiry?, cvc?, name? } | - | The errors to show in place of the field's own, e.g. from your form or a decline. |
| autoAdvance | boolean | true | Move to the expiry when the number is done, and to the code when the expiry is. |
| askName | boolean | false | Ask for the name on the card too. |
| label / labels | ReactNode | null / { number?, expiry?, cvc?, name? } | "Card" / - | The inline row's label (the split group's name for screen readers), and the field labels. |
| disabled | boolean | false | Every field off. |
| now | number | Date.now() | For the expiry check. |
| ref | { focus(field?) } | - | Focus a field, or the first one with an error. Opens the inline fold when needed. |
Accessibility
- Each field has a name (a visible label in split, an aria-label in inline that says the format and the code length) and the autocomplete browsers and password managers fill: cc-number, cc-exp, cc-csc, cc-name.
- The number, expiry and code open the numeric keypad on a phone (inputMode numeric); autocorrect and spellcheck are off.
- An error is tied to its field (aria-invalid, aria-describedby), so it is read when the field gets focus.
- The brand is not only a picture: the number field's description says it ("American Express").
- Inline, the folded expiry and code are inert until they show, so Tab never lands on a hidden field. Moving focus on after the number is a focus change a screen reader announces.
What it handles
- 01Spaces the number the brand's way as it is typed or pasted (4-4-4-4, American Express 4-6-5, Diners 4-6-4) and keeps the caret after the digit you typed, even mid-number; Backspace and Delete step over the spaces.
- 02Shows the brand as a neutral text glyph from the first digits (Visa, Mastercard, American Express, Discover, JCB, UnionPay, Diners), never a logo, and sizes the security code for it.
- 03Checks the number (length and Luhn), the expiry against this month and the code's length, in words a person can act on, under the field they belong to.
- 04Formats the expiry as MM / YY as it is typed, reads an autofilled 12/2027, and steps back over the slash.
- 05Moves focus on when the number and the expiry are done, so the whole card is typed in one go.
- 06Hands back the parsed card on every change (brand, digits, last 4, month and year, complete, errors) and calls onComplete once it all checks out.
States
- First render
- Nothing moves; a saved card shows with its fields open
- none
- Brand found
- The glyph changes to the brand's letters
- instant
- Inline, number checks out
- Expiry and code fold in from the right: the column opens and they slide 6px and fade in
- 180ms ease out
- Inline, number cleared
- They fold back out, while empty
- 120ms ease out
- Field left with an error
- The words show under it and the ring turns red
- instant
- Reduced motion
- The fields appear and go at once
- 0ms
| State | What moves | Timing |
|---|---|---|
| First render | Nothing moves; a saved card shows with its fields open | none |
| Brand found | The glyph changes to the brand's letters | instant |
| Inline, number checks out | Expiry and code fold in from the right: the column opens and they slide 6px and fade in | 180ms ease out |
| Inline, number cleared | They fold back out, while empty | 120ms ease out |
| Field left with an error | The words show under it and the ring turns red | instant |
| Reduced motion | The fields appear and go at once | 0ms |
Works with
When to use
When to use
- Adding or replacing a card in billing settings, inside your own form and save button.
- The card step of your own checkout or a trial sign-up, in one row or as labelled fields.
When not to use
- A whole pay step with email, name, wallets and the pay button: use Payment Form.
- Showing the card on file: use Receipt Card.
Using with AI
Use Copy page for AI above to give your assistant this page, Copy prompt under the install command to have it installed and wired to your data, or connect the Knowns UI MCP server and just ask:
- Add the Knowns UI Card Input to my app and wire it to my own data.
- I need adding or replacing a card in billing settings, inside your own form and save button. Set that up with the Knowns UI Card Input.
- Which Card Input props do I need for a minimal setup? Show me a short example.
With the server connected, your assistant calls get_component({ name: "card-input" }) and search_docs({ query: "card input" }).