Payment card input
Grouped credit card number, expiry, CVC, and optional postal field with live spacing, network detection, and Luhn check.
pnpm dlx shadcn@latest add @haze/payment-card-input---
import { CreditCard, Lock } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";
interface Props {
id: string;
name?: string;
label?: string;
showPostal?: boolean;
required?: boolean;
class?: string;
}
const {
id,
name = id,
label = "Card details",
showPostal = false,
required = false,
class: className,
} = Astro.props;
const fieldCls =
"h-10 w-full min-w-0 bg-transparent px-3 font-mono text-sm tracking-[-0.01em] text-foreground placeholder:font-sans placeholder:text-muted-foreground focus:outline-hidden";
---
<fieldset id={id} data-haze-payment-card class={cn("grid min-w-0 gap-2 border-0 p-0", className)}>
<legend class="flex items-center justify-between gap-2 text-[0.8125rem]/5 font-medium text-foreground">
<span>{label}</span>
<span data-pc-brand class="font-mono text-xs font-normal text-muted-foreground">Card</span>
</legend>
<div class="overflow-hidden rounded-lg border border-frame-input bg-field transition-[border-color,box-shadow] duration-150 ease-snappy hover:border-frame-foreground has-[:is(input,textarea):focus-visible]:border-ring has-[:is(input,textarea):focus-visible]:ring-3 has-[:is(input,textarea):focus-visible]:ring-ring/24">
<div class="relative flex items-center border-b border-border">
<CreditCard class="pointer-events-none absolute left-3 size-4 text-muted-foreground" aria-hidden="true" />
<label for={`${id}-number`} class="sr-only">Card number</label>
<input
id={`${id}-number`}
name={`${name}-number`}
type="text"
inputmode="numeric"
autocomplete="cc-number"
placeholder="1234 5678 9012 3456"
maxlength="19"
required={required}
data-pc-number
class={cn(fieldCls, "pl-9 pr-14")}
/>
<span data-pc-status-badge class="pointer-events-none absolute right-3 font-mono text-[0.6875rem] text-muted-foreground"></span>
</div>
<div class={cn("grid divide-x divide-border", showPostal ? "grid-cols-3" : "grid-cols-2")}>
<div>
<label for={`${id}-expiry`} class="sr-only">Expiration month and year</label>
<input
id={`${id}-expiry`}
name={`${name}-expiry`}
type="text"
inputmode="numeric"
autocomplete="cc-exp"
placeholder="MM / YY"
maxlength="7"
required={required}
data-pc-expiry
class={fieldCls}
/>
</div>
<div class="relative flex items-center">
<label for={`${id}-cvc`} class="sr-only">Security code</label>
<input
id={`${id}-cvc`}
name={`${name}-cvc`}
type="text"
inputmode="numeric"
autocomplete="cc-csc"
placeholder="CVC"
maxlength="4"
required={required}
data-pc-cvc
class={cn(fieldCls, "pr-8")}
/>
<Lock class="pointer-events-none absolute right-3 size-3.5 text-muted-foreground" aria-hidden="true" />
</div>
{
showPostal && (
<div>
<label for={`${id}-zip`} class="sr-only">Postal code</label>
<input
id={`${id}-zip`}
name={`${name}-zip`}
type="text"
autocomplete="postal-code"
placeholder="ZIP"
maxlength="10"
class={fieldCls}
/>
</div>
)
}
</div>
</div>
<p data-pc-hint role="status" class="min-h-4 text-xs text-muted-foreground">
Encrypted before submission. Numbers format automatically.
</p>
</fieldset>
<script>
function luhnOk(digits: string) {
let sum = 0;
let dbl = false;
for (let i = digits.length - 1; i >= 0; i--) {
let n = Number(digits[i]);
if (dbl) {
n *= 2;
if (n > 9) n -= 9;
}
sum += n;
dbl = !dbl;
}
return sum % 10 === 0;
}
function initPaymentCards() {
document.querySelectorAll<HTMLElement>("[data-haze-payment-card]:not([data-enhanced])").forEach((root) => {
root.setAttribute("data-enhanced", "");
const numIn = root.querySelector<HTMLInputElement>("[data-pc-number]");
const expIn = root.querySelector<HTMLInputElement>("[data-pc-expiry]");
const brandEl = root.querySelector<HTMLElement>("[data-pc-brand]");
const badgeEl = root.querySelector<HTMLElement>("[data-pc-status-badge]");
const hintEl = root.querySelector<HTMLElement>("[data-pc-hint]");
numIn?.addEventListener("input", () => {
const raw = numIn.value.replace(/\D/g, "").slice(0, 16);
const groups = raw.match(/.{1,4}/g) ?? [];
numIn.value = groups.join(" ");
if (brandEl) {
if (raw.startsWith("4")) brandEl.textContent = "Visa";
else if (/^(5[1-5]|2[2-7])/.test(raw)) brandEl.textContent = "Mastercard";
else if (/^3[47]/.test(raw)) brandEl.textContent = "Amex";
else brandEl.textContent = "Card";
}
if (raw.length >= 13 && badgeEl && hintEl) {
const valid = luhnOk(raw);
badgeEl.textContent = valid ? "Valid" : "Check";
badgeEl.className = `pointer-events-none absolute right-3 font-mono text-[0.6875rem] ${valid ? "text-success" : "text-destructive"}`;
hintEl.textContent = valid ? "Card checksum verified." : "Card number checksum does not match.";
} else if (badgeEl && hintEl) {
badgeEl.textContent = "";
hintEl.textContent = "Encrypted before submission. Numbers format automatically.";
}
});
expIn?.addEventListener("input", () => {
const d = expIn.value.replace(/\D/g, "").slice(0, 4);
expIn.value = d.length > 2 ? `${d.slice(0, 2)} / ${d.slice(2)}` : d;
});
});
}
initPaymentCards();
if (!document.documentElement.hasAttribute("data-haze-payment-card-listener")) {
document.documentElement.setAttribute("data-haze-payment-card-listener", "");
document.addEventListener("astro:page-load", initPaymentCards);
}
</script>When to use
- Checkout forms and billing payment-method editors
- Updating workspace credit cards with immediate format validation
When not to use
- Simple amount currency entry belongs in price-input
Installation
Add the Haze registry to components.json once, then install Payment card input with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/payment-card-inputnpx shadcn@latest add @haze/payment-card-inputyarn dlx shadcn@latest add @haze/payment-card-inputbunx --bun shadcn@latest add @haze/payment-card-inputFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| id* | string | – | Prefix id for grouped card fields. |
| label | string | "Card details" | Fieldset legend text. |
| showPostal | boolean | false | Adds an optional ZIP / postal code column. |
| required | boolean | false | Marks number, expiry, and CVC required. |
Accessibility
- Uses standard autocomplete tokens (cc-number, cc-exp, cc-csc, postal-code) for browser autofill.
- Explicit labels for every sub-input inside an accessible fieldset legend.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
Install and use
Install the Haze UI component "Payment card input" with `npx shadcn@latest add @haze/payment-card-input`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "http://localhost:4329/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import PaymentCardInput from "@/components/haze/ui/payment-card-input.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: http://localhost:4329/ui/payment-card-input.md
Customize with real content
Adapt the Haze payment-card-input ui for our product. Replace the demo values of these props with our real content: id, label, showPostal, required. Keep the layout, the heading level rules and these accessibility details: Uses standard autocomplete tokens (cc-number, cc-exp, cc-csc, postal-code) for browser autofill. Explicit labels for every sub-input inside an accessible fieldset legend. Do not add raw color classes; use the Haze tokens.
Choose the right block
Before using payment-card-input, check it fits: use it when checkout forms and billing payment-method editors; updating workspace credit cards with immediate format validation. Avoid it when simple amount currency entry belongs in price-input. Alternatives: @haze/price-input, @haze/input-group, @haze/field.
Dependencies
- npm: @lucide/astro
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.