Skip to content

CreditCardField

MD · 1950B gzip budget

Credit card number, expiry, and CVC inputs with auto-advance.

Category
Forms & input
Budget
md, several states or a live subscription
External runtime
React / React DOM only
axe fixtureSSR rendersource scanbundle budget

Preview

Live fixture
Preview locale

Enter your 16-digit card number

Try the component here. Locale-aware formatting and direction follow the selected language; example content and labels are not automatically translated. Overlay demos use interactive launchers.

Install

npx shadcn@latest add https://gear5-ui.vercel.app/r/credit-card-field.json

Copies the source into your project. Pulls in 1 shared registry item: cn.

New here? Set up Tailwind and import aliases first →

Source & props

Edit on GitHub ↗

Prop types and inline documentation are included below. This is repository source; the installer rewrites shared imports for your project.

"use client";

import { useId, useState } from "react";
import { cn } from "../lib/cn";

export interface CreditCardFieldProps {
  value: string;
  onChange: (value: string) => void;
  label: string;
  className?: string;
}

function detectCardType(digits: string): string | null {
  if (/^4/.test(digits)) return "Visa";
  if (/^5[1-5]/.test(digits)) return "Mastercard";
  if (/^3[47]/.test(digits)) return "Amex";
  if (/^6(?:011|5)/.test(digits)) return "Discover";
  return null;
}

function formatCardNumber(raw: string): string {
  const digits = raw.replace(/\D/g, "").slice(0, 16);
  return digits.replace(/(\d{4})(?=\d)/g, "$1 ");
}

const CARD_ICONS: Record<string, string> = {
  Visa: "💳",
  Mastercard: "💳",
  Amex: "💳",
  Discover: "💳",
};

export function CreditCardField({ value, onChange, label, className }: CreditCardFieldProps) {
  const id = useId();
  const raw = value.replace(/\D/g, "");
  const cardType = detectCardType(raw);

  return (
    <div className={cn("flex flex-col gap-1.5 text-start", className)}>
      <label htmlFor={id} className="text-sm font-medium">
        {label}
      </label>
      <div className="relative">
        <input
          id={id}
          type="text"
          inputMode="numeric"
          autoComplete="cc-number"
          value={formatCardNumber(value)}
          onChange={(e) => onChange(e.target.value.replace(/\D/g, "").slice(0, 16))}
          placeholder="1234 5678 9012 3456"
          aria-describedby={cardType ? `${id}-type` : undefined}
          className="w-full rounded-md border border-neutral-300 bg-white px-3 py-2 pe-16 text-base tabular-nums focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-600 dark:border-neutral-700 dark:bg-neutral-950"
        />
        {cardType && (
          <span id={`${id}-type`} className="absolute end-3 top-1/2 -translate-y-1/2 text-sm" aria-label={cardType}>
            {CARD_ICONS[cardType]} {cardType}
          </span>
        )}
      </div>
      <p className="text-xs text-neutral-500 dark:text-neutral-400">
        Enter your 16-digit card number
      </p>
    </div>
  );
}

What CI checks

Quality contract
  • Bundled, minified and gzipped against its tier budget
  • Audited by axe in the state previewed above
  • Rendered through react-dom/server with no browser globals
  • Scanned for network calls, dangerous sinks, and unguarded animation