v1.5

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

pnpm dlx @create-ui/cli add credit-card-input

The command also adds input-group and the useCreditCardInput hook.

Usage

import { CreditCardInput } from "@/components/ui/credit-card-input"
<Field>
  <FieldLabel htmlFor="card-number">Card Number</FieldLabel>
  <CreditCardInput
    id="card-number"
    onValueChange={(value, { cardType, isValid }) =>
      console.log(cardType, isValid)
    }
  />
</Field>

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.

<CreditCardInput
  onValueChange={(digits, { cardType, isValid, isPotentiallyValid }) => {
    // cardType: "visa" | "mastercard" | "american-express" | ... | null
    // isValid: the number is complete and passes validation
    // isPotentiallyValid: the digits so far could still become valid
  }}
/>

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.

import { VisaBadge } from "@create-ui/assets/payments"
 
;<CreditCardInput badges={{ visa: VisaBadge }} />

Seeding a value

defaultValue is a raw digit string. It seeds an uncontrolled field.

<CreditCardInput defaultValue="4242424242424242" />

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.

const [card, setCard] = React.useState("")
 
<CreditCardInput value={card} onValueChange={(digits) => setCard(digits)} />

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:

<CreditCardInput autoComplete="cc-number" />

Accessibility

CreditCardInput renders a real text input inside an InputGroup, with inputMode="numeric".

KeyDescription
TabMoves focus into the input.
Shift + TabMoves focus out of the input.
EnterSubmits the surrounding <form> when present.

ARIA notes:

  • Give the input a name with FieldLabel htmlFor, aria-label or aria-labelledby. Do not rely on the placeholder, which shows the mask.
  • The component sets no autocomplete value. Pass autoComplete="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 FieldError when the number is invalid, so the message does not depend on the icon.
  • aria-invalid and error styling follow invalid, directly or from a parent Field. The validity icon keeps showing its own state under a form error.

Styling

Tailwind override: pass className to restyle the group:

<CreditCardInput className="w-[360px]" />

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.
  • Input Group: the shell CreditCardInput is 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

PropTypeDefaultDescription
defaultValuestring-Raw digit string. Seeds an uncontrolled field.
valuestring-Controlled value: raw digits (masked text also works). "" clears it.
onChangeReact.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.
placeholderstringthe maskDefaults to the mask string. Pass it to override.
invalidboolean-Marks the field invalid. Inherits from Field.
disabledboolean-Disables the field. Inherits from Field.
loadingboolean-Shows a spinner. Inherits from Field.
showLeadingIconbooleantrueShows the card icon at the start of the field.
leadingReactNodecard iconReplaces the leading icon.
showBadgebooleantrueShows the brand badge cell.
showValidationIconbooleantrueShows the validity icon.
badgesPartial<Record<CardType, CardBadge>>the nine built-in badgesReplaces the brand-to-badge map. Not merged.
classNamestring-Tailwind classes merged on the group.
refReact.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

VariantOptionsDefaultDescription
size"xs" "sm" "md""sm"Height and type scale. Omit it inside Field; the size comes from there.

CreditCardDetails

FieldTypeDescription
cardTypeCardType | nullThe detected brand, or null.
isValidbooleanThe number is complete and valid.
isPotentiallyValidbooleanThe digits so far could still become a valid number.