Card
Credit and debit card numbers. Visa, Mastercard, Amex and Troy are detected; Amex switches the pattern.
← All examples·card
Built-in validation: Luhn check
Length for the network (Amex 15, others 16) and the Luhn checksum. Every single-digit error and most swapped neighbours are caught; the error is card.invalid.
Preview
Form value: -
Display: -
Details
- Pattern:
9999 9999 9999 9999 · Amex 9999 999999 99999 - Preset:
"card" - Form value:
"4111111111111111" - Error keys:
card.incomplete,card.invalid - Validation: length for the network and Luhn.
- The form value is digits only.
Card networks
The network comes from the leading digits; the pattern, length and CVV digits follow it.
| Network | Starts with | Length | Grouping | CVV | getCardType |
|---|---|---|---|---|---|
| Visa | 4 | 16 | 4-4-4-4 | 3 | "visa" |
| Mastercard | 51–55, 2221–2720 | 16 | 4-4-4-4 | 3 | "mastercard" |
| American Express | 34, 37 | 15 | 4-6-5 | 4 | "amex" |
| Troy | 9792 | 16 | 4-4-4-4 | 3 | "troy" |
| Other | — | 16 | 4-4-4-4 | 3 | "unknown" |
Customization
Showing the card type
Read the network from the form value with getCardType.
import { getCardType, useViraMask } from "@virastack/mask"
const { card } = useViraMask(form, { card: "card" })
const type = getCardType(form.watch("card")) // "visa" | "mastercard" | "amex" | "troy" | "unknown"Move on when complete
onComplete runs when the mask is full. You decide where focus goes.
const { card, expiry } = useViraMask(form, {
card: { preset: "card", onComplete: () => form.setFocus("expiry") },
expiry: "expiry",
})For the hook API see useViraMask(); for your own pattern see custom mask.