# 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.

- Kind: Component (Billing)
- Docs: https://ui.minidev.pro/docs/credit-card-input
- Source (registry JSON with the full file): https://ui.minidev.pro/r/credit-card-input.json
- License: MIT. One file, yours to edit after install.

## 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.

## Installation

### shadcn CLI (copies the source into your project)

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

Writes `components/ui/credit-card-input.tsx` plus the MiniDev files it is built from (input) and installs its npm dependencies. Older CLI versions: `npx shadcn@latest add https://ui.minidev.pro/r/credit-card-input.json`.

### npm package

```bash
npm i minidev-ui-kit lucide-react motion
```

Add the tokens to your global stylesheet once:

```css
@import "tailwindcss";
@import "minidev-ui-kit/styles.css";
@source "../node_modules/minidev-ui-kit";
```

Next.js also needs `transpilePackages: ["minidev-ui-kit"]` in next.config.

## Import

After the shadcn install:

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

From the npm package:

```tsx
import { CreditCardInput } from "minidev-ui-kit/ui/credit-card-input"
```

## Usage

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

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

## Props

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| `onChange` | `(value: CreditCardValue) => void` | - | no | Called on every edit with the parsed values. |
| `defaultValue` | `Partial<Pick<CreditCardValue, "number" \| "expiry" \| "cvc" \| "name" \| "postalCode">>` | `{}` | no | Initial field values; `number` and `expiry` may include spaces or a slash. |
| `showName` | `boolean` | `true` | no | Show the name on card field. |
| `showPostalCode` | `boolean` | `true` | no | Show the postal code field. |
| `showBrands` | `boolean` | `true` | no | Show the accepted brands, the detected one highlighted. |
| `label` | `string` | `"Card details"` | no | Visible label of the card fields group. Defaults to "Card details". |
| `hint` | `React.ReactNode` | `"Test with 4242 4242 4242 4242 and an...` | no | Helper line shown while there is no error. |
| `disabled` | `boolean` | `false` | no |  |
| `className` | `string` | - | no |  |

## 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.

```tsx
<CreditCardInput showName={false} showPostalCode={false} label="Payment method" hint="Charged on the 1st of each month." />
```

### Reading the value

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

```tsx
function PayDemo() {
  const [card, setCard] = React.useState<CreditCardValue | null>(null)
  const brand = card && card.brand !== "unknown" ? card.brand[0].toUpperCase() + card.brand.slice(1) : null
  return (
    <form
      className="flex w-full max-w-sm flex-col gap-4"
      onSubmit={(e) => e.preventDefault()}
    >
      <CreditCardInput onChange={setCard} showBrands={false} hint="Your card is tokenized by the payment provider; Lumen never sees the number." />
      <Button type="submit" size="lg" className="w-full" disabled={!card?.valid}>
        <LockIcon /> Pay $12,400.00{brand ? ` with ${brand}` : ""}
      </Button>
    </form>
  )
}
```

### 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.

```tsx
<CreditCardInput
  defaultValue={{ name: "Maya Chen", number: "4242 4242 4242 4242", expiry: "12/28", cvc: "123", postalCode: "98101" }}
  disabled
  label="Card on file"
  hint="Used for the Northwind workspace."
/>
```

## Keyboard interactions

| Key | Action |
| --- | --- |
| `Tab` or `Shift+Tab` | Moves between name, number, expiry, CVC and postal code. |
| `Backspace` | In 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.

## FAQ

### 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.

## Rules for generating code

- Use semantic tokens only (bg, surface, sunken, border, fg, fg-muted, fg-subtle, ink, accent, success, warning, danger). Never raw hex or palette steps like blue-500.
- Shadows only from the set: shadow-xs, shadow-sm, shadow-raised, shadow-md, shadow-lg, shadow-overlay.
- Numbers use tabular-nums. Dark mode is the `dark` class on <html>; components never branch on theme.

## Dependencies

- npm: `lucide-react`, `motion`
- Built from: [input](https://ui.minidev.pro/docs/input/md)

## Related

- [Card](https://ui.minidev.pro/docs/card/md): Shadcn compatible card with header, title, description, action, content and footer slots, a compact size and edge to edge images. Built for Tailwind v4.
- [Payment Method Card](https://ui.minidev.pro/docs/payment-method-card/md): A saved card row showing the card brand, last four digits and expiry date in monospace, with an Edit button. Fits the payment methods area of billing.
- [Add Card Form](https://ui.minidev.pro/docs/add-card-form/md): Compact payment form for card number, expiry and CVC with correct autocomplete hints, numeric input mode and a full width save button. Ready for your processor.
- [Checkout Summary](https://ui.minidev.pro/docs/checkout-summary/md): Tailwind order summary card listing line items and prices, a divided total row and a full width Pay now button. Fits the side column of a checkout.
- [Billing Address](https://ui.minidev.pro/docs/billing-address/md): Tailwind billing address fieldset with a multiline street field and a responsive row for city, state and postal code. Drops into checkout forms.
- [Cancel Flow](https://ui.minidev.pro/docs/cancel-flow/md): Two step subscription cancellation flow: ask for a reason with radio options, then confirm with a destructive button or go back. Tells users what they keep.

---

All 557 components: https://ui.minidev.pro/llms.txt (full text: https://ui.minidev.pro/llms-full.txt).
