Skip to content

Credit Card Input

React credit card input that groups the number by brand, runs a Luhn check, formats expiry as MM / YY and sizes the CVC for Amex. UI only, no payments.

src/registry/ui/credit-card-input.tsx/r/credit-card-input.json

Installation

bash
npx shadcn@latest add @minidev/credit-card-input

MiniDev UI is in the shadcn registry directory, so the short form works with no setup. Older CLI versions can use the full URL: npx shadcn@latest add https://ui.minidev.pro/r/credit-card-input.json

Usage

tsx
import { CreditCardInput } from "@/components/ui/credit-card-input"

export function Example() {
  return (
    <CreditCardInput />
  )
}

Exports CreditCardInput luhn

API reference

CreditCardInput props
PropTypeDefault
onChangeCalled on every edit with the parsed values.(value: CreditCardValue) => void-
defaultValueInitial field values; `number` and `expiry` may include spaces or a slash.Partial<Pick<CreditCardValue, "number" | "expiry" | "cvc" | "name" | "postalCode">>{}
showNameShow the name on card field.booleantrue
showPostalCodeShow the postal code field.booleantrue
showBrandsShow the accepted brands, the detected one highlighted.booleantrue
labelVisible label of the card fields group. Defaults to "Card details".string"Card details"
hintHelper line shown while there is no error.React.ReactNode"Test with 4242 4242 4242 4242 and an...
disabledbooleanfalse
classNamestring-

When to use

The number groups by brand (Visa, Mastercard and Discover 4-4-4-4, Amex 4-6-5), the brand shows as a text badge, expiry formats to MM / YY and advances on its own, and the CVC expects 4 digits for Amex. Fields carry cc-* names and autocomplete hints and aria-invalid, and errors appear once a field is left. It formats and checks input in the browser and does not process, tokenize or transmit payments: for real payments prefer your provider's hosted fields (Stripe Elements and the like) for PCI scope, and use this for demos, prototypes or providers that accept raw values. For a saved card use PaymentMethodCard; for a full add-card flow use AddCardForm.

onChange receives { number, expiry, cvc, brand, name, postalCode, valid }; luhn(digits) is exported for your own checks.

Keyboard interactions

Keyboard interactions
KeyAction
TaborShiftTabMoves between name, number, expiry, CVC and postal code.
BackspaceIn an empty expiry, CVC or postal code field, moves focus to the end of the previous field.

Accessibility

  • Number, expiry, CVC and postal code sit in a role="group" labelled by the visible label ("Card details"), and each has its own screen-reader-only <label>, such as "Expiry date, month and year".
  • Every field with a shown error gets aria-invalid; the first one also gets aria-describedby pointing at the message line. That line is aria-live="polite", so errors and "Card details look complete." are announced.
  • A detected brand is announced through a polite live region ("Visa detected"), and gets aria-current in the accepted cards list.
  • Focus moves on by itself when the number is complete and passes Luhn, when the expiry has 4 digits, and from a full CVC to the postal code when it is shown. Under reduced motion the brand badge swaps without the spring.

Frequently asked questions

Does CreditCardInput charge the card or send data to a payment provider?

No. It formats and checks input in the browser and sends nothing. onChange gives you { number, expiry, cvc, brand, name, postalCode, valid } on every edit; what you do with it is up to you. For live payments, hosted fields from your provider keep raw card data off your server.

What does a form submit send?

The fields carry autofill names, cc-name, cc-number, cc-exp, cc-csc and postal-code, so FormData and Server Actions get them. The values are as displayed: the number keeps its spaces and the expiry reads MM / YY. For clean digits, keep the latest onChange value in state and send that, and enable the pay button only when card.valid is true. Hidden name or postal code fields are not rendered, so they are not sent.

When do errors appear?

When a field loses focus, with two exceptions: a number at its brand's longest length (16 for Mastercard, 15 for Amex, 19 for Visa) that fails the Luhn check, and a 4-digit expiry that is invalid or in the past, are flagged at once. Only the first error in field order is shown, in one line under the fields.

Which card brands does it detect?

Visa, Mastercard (including 2-series numbers), Amex and Discover, from the number's prefix. Amex groups 4-6-5 and needs a 4-digit CVC, the others group 4-4-4-4, and any other number is treated as a generic card of 12 to 19 digits. luhn(digits) is exported for your own checks.

Dependencies

npm

lucide-reactmotion

Built from

Examples

Card number only

Hide the name and postal code fields when the provider does not need them; the brand badges stay as a hint of what is accepted.

Reading the value

onChange reports every edit with the parsed fields and a valid flag, which is what gates the pay button.

Saved card, disabled

defaultValue fills the fields and disabled locks them, the usual way to show the card on file before a change card action.

All pricing and billing components