Skip to content
WANCRAFT

F.143Forms & Inputs

Card Input

Payment card fields with brand detection, live formatting, Luhn checks and a card preview that flips for the CVC.

NewReactMotionTailwindadvanced

Overview

Card Input is the interface for entering a payment card, and only the interface: it never sends, stores or logs anything, and real payments should go through your processor's PCI-compliant hosted fields. The number field detects Visa, Mastercard, American Express and Discover from the IIN prefix, formats as you type (4-4-4-4, or 4-6-5 for Amex) while keeping the caret where you expect it, and validates with the Luhn checksum. Expiry auto-inserts the slash, accepts autofilled MM/YYYY and rejects past dates; the CVC length follows the brand. Fields advance automatically when complete and Backspace in an empty field steps back. A preview card mirrors what you type, outlines the field in focus and flips to its back while the CVC is focused (Amex keeps its code on the front). Brands are shown as plain text badges, not trademark logos. onChange reports structured data: digits, brand, month, year, CVC, name, errors and a complete flag.

01Installation

Method 01 · Registry

CLI verified

Uses the shadcn CLI in a project initialised with shadcn init. Installs the file to components/ui/card-input.tsx and adds its dependencies.

npx shadcn@latest add https://wancraft.my/r/card-input.json

Method 02 · Copy and paste

  1. 1. Install the dependencies below.
  2. 2. Copy the source from the Code tab into components/ui/card-input.tsx.
  3. 3. Import it and adjust props.
npm install motion
Dependencies
motion
Peer
react ≥ 18, tailwindcss ≥ 4
Added
13 Oct 2026
Updated
—

02Usage

The default configuration. Change properties above to generate a tailored example in the Usage tab.

example.tsx
import { CardInput } from "@/components/ui/card-input";

export function CardInputExample() {
  return (
    <div className="w-full max-w-2xl">
      <CardInput onChange={(card) => console.log(card.complete, card.brand)} />
    </div>
  );
}

03Props

PropTypeDefaultDescription
onChange(value: CardInputValue) => void—Structured data after every edit: { number (digits), brand, expiryMonth, expiryYear, cvc, name, errors, complete }.
showPreviewbooleantrueShow the animated card preview.
flipOnCvcbooleantrueFlip the preview to its back while the CVC is focused (not for Amex).
autoAdvancebooleantrueFocus the next field when one is complete and valid.
requireNamebooleantrueTreat the cardholder name as required.
cardStyle"graphite" | "accent" | "paper""graphite"Preview card finish.
accentstring"#FF7448"Focus ring, caret and accent-finish colour.
numberPlaceholderstring"4242 4242 4242 4242"Number placeholder; keep it a public test number.
disabledbooleanfalseDisables every field.
...propsHTMLAttributes<HTMLDivElement>—Forwarded to the root element.

04Features

  • 01Interface only: no network calls, storage or logging; data leaves only through your onChange
  • 02IIN brand detection (Visa, Mastercard incl. 2-series, Amex, Discover) shown as generic text badges
  • 03Formats 4-4-4-4 or 4-6-5 as you type, with caret preservation and separator-aware deletion
  • 04Luhn validation; expiry month/past-date checks; CVC length follows the brand
  • 05Auto-advance on completion, Backspace steps back; accepts autofilled MM/YYYY
  • 06Preview card mirrors input, outlines the focused field and flips for the CVC with a spring
  • 07Exported helpers: detectCardBrand and luhnCheck
motion · springinput · keyboardinput · focus

05Accessibility

  • Every field has a visible <label>; the group is a <fieldset> with a visually hidden legend.
  • Correct autocomplete tokens (cc-number, cc-exp, cc-csc, cc-name) and inputMode="numeric" so browsers and password managers can fill the card.
  • Errors set aria-invalid and are linked with aria-describedby; they appear after leaving a field (or as soon as a full-length entry is wrong), never while typing a partial value.
  • The detected brand is announced in a polite live region; the preview card is aria-hidden because it only mirrors the fields.
  • prefers-reduced-motion swaps the 3D flip for an instant face change.

06Limitations

  • Not a payment integration and not PCI scope reduction: in production use your processor's hosted fields or tokenisation, and never log or store card data yourself.
  • Detection covers four networks; other cards are accepted as "Card" with 16–19 digits and a 3-digit code.