BlockDex

Components

Registries

Free blocks

Method

BlockDex

Components

Registries

Free blocks

Method

Open API

BlockDex

Components

Registries

Free blocks

Method

Open API

Components/billui/card-input

Card Input

billui/card-input
FreeComponent

Native formatted card inputs with auto-formatting, brand detection, and masking.

Live preview

live · rendered by the registry
Rendered by billui on their own page — this is the component running, not a screenshot of it.

Install it

npx shadcn@latest add https://billui.com/r/card-input.json

Source

src/registry/ui/card-input.tsxbillui.com/r/card-input.json · first 6 KB
1"use client";
2
3import * as React from "react";
4import { useControllableState } from "@/lib/use-controllable-state";
5import { cn } from "@/lib/utils";
6import { type CardBrand, CardBrandIcons } from "./card-icons";
7
8interface CardInputContextValue {
9 brand: CardBrand;
10 setBrand: (brand: CardBrand) => void;
11}
12
13const CardInputContext = React.createContext<CardInputContextValue | null>(
14 null,
15);
16
17function useCardInputContext() {
18 return React.useContext(CardInputContext);
19}
20
21function detectCardBrand(cardNumber: string): CardBrand {
22 const number = cardNumber.replace(/\s/g, "");
23
24 if (!number) return "unknown";
25
26 // Visa: starts with 4
27 if (/^4/.test(number)) return "visa";
28
29 // Mastercard: starts with 51-55 or 2221-2720
30 if (/^5[1-5]/.test(number) || /^2[2-7]/.test(number)) return "mastercard";
31
32 // Amex: starts with 34 or 37
33 if (/^3[47]/.test(number)) return "amex";
34
35 // Discover: starts with 6011, 644-649, 65
36 if (/^6011|^64[4-9]|^65/.test(number)) return "discover";
37
38 // Diners Club: starts with 36, 38, or 300-305
39 if (/^36|^38|^30[0-5]/.test(number)) return "diners";
40
41 // JCB: starts with 35
42 if (/^35/.test(number)) return "jcb";
43
44 // UnionPay: starts with 62
45 if (/^62/.test(number)) return "unionpay";
46
47 return "unknown";
48}
49
50function validateLuhn(cardNumber: string): boolean {
51 const number = cardNumber.replace(/\s/g, "");
52
53 if (!number || !/^\d+$/.test(number)) return false;
54
55 let sum = 0;
56 let isEven = false;
57
58 // Loop through digits from right to left
59 for (let i = number.length - 1; i >= 0; i--) {
60 let digit = parseInt(number[i], 10);
61
62 if (isEven) {
63 digit *= 2;
64 if (digit > 9) {
65 digit -= 9;
66 }
67 }
68
69 sum += digit;
70 isEven = !isEven;
71 }
72
73 return sum % 10 === 0;
74}
75
76function isCardNumberComplete(cardNumber: string, brand: CardBrand): boolean {
77 const number = cardNumber.replace(/\s/g, "");
78 const maxLength = getCardMaxLength(brand);
79 return number.length === maxLength;
80}
81
82function validateCardNumber(cardNumber: string): {
83 isValid: boolean;
84 isComplete: boolean;
85 brand: CardBrand;
86} {
87 const brand = detectCardBrand(cardNumber);
88 const isComplete = isCardNumberComplete(cardNumber, brand);
89 const isValid = isComplete && validateLuhn(cardNumber);
90
91 return { isValid, isComplete, brand };
92}
93
94function getCardMaxLength(brand: CardBrand): number {
95 // Amex has 15 digits, others have 16
96 return brand === "amex" ? 15 : 16;
97}
98
99function getCvcMaxLength(brand: CardBrand): number {
100// … truncated

What it pulls in

Other registry items

  • https://billui.com/r/use-controllable-state.json
  • https://billui.com/r/utils.json
  • https://billui.com/r/card-icons.json

Files it writes

  • src/registry/ui/card-input.tsx

Facts

Registry
billui
Type
registry:ui
Access
Free
Files written
1
Licence
MIT
First indexed
2026-08-03
Last confirmed
yesterday

Go to the source

Docs on billui billui.com commet-labs/billui on GitHub Raw registry item This item as JSON

More from billui

All 13 items
Same registry, same kind — the ones you would have found next
ComponentRegistryKindAccessInstallsCommand
Animated Usage Cardanimated-usage-cardbilluiComponentsFree2 deps
Billing Addressbilling-addressbilluiComponentsFreeno deps
Card Iconscard-iconsbilluiComponentsFree1 dep
Invoice Cardinvoice-cardbilluiComponentsFree2 deps
Payment Methodpayment-methodbilluiComponentsFree2 deps
Plan Cardplan-cardbilluiComponentsFree2 deps
Plan Groupplan-groupbilluiComponentsFree1 dep
Pricing Tablepricing-tablebilluiComponentsFree2 deps
BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex

BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex

BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex