Credit Card Input
Masked card number field that detects the card brand, shows its badge and a validity icon, and reports the digits with their details.
Description
CreditCardInput collects a card number. It masks the digits in groups of four, switches to the American Express grouping (4-6-5) when the number starts with 34 or 37, detects the card brand, shows a brand badge at the end of the field, and shows a validity icon. Brand detection and validation use card-validator.
onValueChange reports the digits without separators, plus { cardType, isValid, isPotentiallyValid }.
It covers the card number only. Build the expiry and the security code as separate Field and Input controls.
For plain text, use Input. For custom masked fields, compose InputGroup. For one-time codes, use InputOTP.
Installation
The command also adds input-group and the useCreditCardInput hook.
Usage
Examples
With a label
Wrap the input in Field, add a FieldLabel, and tie them together with id and htmlFor. Field does not wire the id for you. size, invalid, disabled and loading flow from Field when you leave them unset on the input.
Reading the value
onValueChange fires for typed changes only. It does not fire on mount, so a defaultValue stays quiet, and it does not fire when a parent sets value. The first argument is the digits only.
Validity icon
The icon is hidden while the field is empty or loading. It shows a check when the number is valid, and an error icon only when the number can no longer become valid. Turn it off with showValidationIcon={false}.
Brand badge
Supported brands: visa, mastercard, american-express, diners-club, discover, jcb, maestro, unionpay and elo. A number with no detected brand shows a generic card icon. Hide the badge cell with showBadge={false}.
badges replaces the whole map from brand to badge. It is not merged with the defaults, so brands you leave out fall back to the generic icon.
Seeding a value
defaultValue is a raw digit string. It seeds an uncontrolled field.
Controlled
Pass value with onValueChange to own the value in the parent. value takes the raw digits that onValueChange emits (masked text also works). Setting value from the parent updates the field without firing onValueChange again, and "" clears it.
With React Hook Form, wire it through Controller so the form stores the digits and reset() refills the field. register() works for submit only: it collects the masked text, and reset() and setValue() do not update the field.
Autofill
The component sets no autoComplete value. Pass one so browsers can fill the field:
Accessibility
CreditCardInput renders a real text input inside an InputGroup, with inputMode="numeric".
| Key | Description |
|---|---|
Tab | Moves focus into the input. |
Shift + Tab | Moves focus out of the input. |
Enter | Submits the surrounding <form> when present. |
ARIA notes:
- Give the input a name with
FieldLabel htmlFor,aria-labeloraria-labelledby. Do not rely on the placeholder, which shows the mask. - The component sets no
autocompletevalue. PassautoComplete="cc-number"yourself, as shown above. - The brand badge and the validity icon are drawn as icons, and the page does not claim how screen readers announce them. Show a text error with
FieldErrorwhen the number is invalid, so the message does not depend on the icon. aria-invalidand error styling followinvalid, directly or from a parentField. The validity icon keeps showing its own state under a form error.
Styling
Tailwind override: pass className to restyle the group:
Data slots: the component uses the InputGroup slots:
data-slot="input-group"on the group.data-slot="input-group-slot"on the slot that holds the icon, the input and the validity icon.data-slot="input-group-control"on the input.data-slot="input-group-card"on the brand badge cell.data-slot="input-group-helper-icon"on the validity icon.
Related Components
- Input Group: the shell
CreditCardInputis built from. - Field: adds the label, description and error footer.
- Input OTP: for one-time codes split into slots.
- Phone Input and Date Input: other masked fields built on
InputGroup.
API Reference
CreditCardInput
Extends React.ComponentProps<"input"> minus size and type. Standard attributes such as id, name, placeholder, required and aria-* reach the inner <input>. Seed an uncontrolled field with defaultValue, or control it with value and onValueChange.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
defaultValue | string | - | Raw digit string. Seeds an uncontrolled field. |
value | string | - | Controlled value: raw digits (masked text also works). "" clears it. |
onChange | React.ChangeEventHandler<HTMLInputElement> | - | Fires once per accepted keystroke with the masked text. For register(). |
onValueChange | (value: string, details: CreditCardDetails) => void | - | Receives the digits without separators and the details. Typed changes only. |
placeholder | string | the mask | Defaults to the mask string. Pass it to override. |
invalid | boolean | - | Marks the field invalid. Inherits from Field. |
disabled | boolean | - | Disables the field. Inherits from Field. |
loading | boolean | - | Shows a spinner. Inherits from Field. |
showLeadingIcon | boolean | true | Shows the card icon at the start of the field. |
leading | ReactNode | card icon | Replaces the leading icon. |
showBadge | boolean | true | Shows the brand badge cell. |
showValidationIcon | boolean | true | Shows the validity icon. |
badges | Partial<Record<CardType, CardBadge>> | the nine built-in badges | Replaces the brand-to-badge map. Not merged. |
className | string | - | Tailwind classes merged on the group. |
ref | React.Ref<HTMLInputElement> | - | Ref to the inner input. |
There is no trailing prop. The end of the field belongs to the validity icon and the brand badge.
Variants
| Variant | Options | Default | Description |
|---|---|---|---|
size | "xs" "sm" "md" | "sm" | Height and type scale. Omit it inside Field; the size comes from there. |
CreditCardDetails
| Field | Type | Description |
|---|---|---|
cardType | CardType | null | The detected brand, or null. |
isValid | boolean | The number is complete and valid. |
isPotentiallyValid | boolean | The digits so far could still become a valid number. |