Custom mask
Pattern syntax, custom tokens, optional and fixed parts, dynamic masks, number and date options.
Pattern syntax
| Syntax | Meaning |
|---|---|
9 | Digit |
a | Letter (Turkish letters included) |
* | Letter or digit |
\ | The next character is literal: \9 shows a 9 |
[ ] | Optional part; the mask counts as complete without it |
{ } | Fixed part; shown and kept in the form value ({TR}, {+}) |
Other characters are display only. Define your own characters with tokens; transform changes letter case.
Form value: -
Display: -
Dynamic masks
With an array, the first pattern that fits the whole value wins. A function gets the value and ctx.from (the linked field's value) and returns a pattern.
useViraMask(form, {
// the first pattern that fits wins
code: { mask: ["999-999", "9999-9999"] },
// pick the pattern from the value or a linked field
card: {
mask: (value) => (/^3[47]/.test(value) ? "9999 999999 99999" : "9999 9999 9999 9999"),
},
// clean the input before it is placed
phone: { mask: "(999) 999 99 99", normalize: (value) => value.replace(/^0+/, "") },
})Number, currency and date
mask, number, currency ve date exclude each other; passing two is a compile error.
number:min,max,scale(fraction digits),signed,locale(defaulten-US; use the preset withcountry: "TR"for Turkish separators). Max applies while typing, min on blur. Withsigned, a negative max also applies on blur.currency: an ISO code or{ code, locale, min, max, scale }. Separators and symbol come from Intl. Without a code:1,234.56, no symbol; Turkish lira:{ preset: "currency", country: "TR" }.date:format,min,max("today", ISO or aDate),minAge. Full dates store ISO in the form.
Form value: -
Display: -
definePreset
Define your team's mask once. The name prefixes its error keys (orderNo.incomplete). The hook, the server and tests use the same object.
// masks.ts — shared by the client and the server
import { definePreset } from "@virastack/mask/server"
export const orderNo = definePreset("orderNo", {
mask: "aa-9999",
transform: "uppercase",
validate: (value) => !value.startsWith("XX") || "orderNo.reserved",
})
// form
useViraMask(form, { orderNo, returnNo: { preset: orderNo } })