useViraMask()
Mask several fields with one hook. The form keeps the raw value; the input shows the mask.
Basic usage
The first argument is what useForm() returns, the second is the schema. Types come from the form; you don't write them again.
Form value: -
Display: -
Schema
Keys are field paths of your form. An unknown field, an unknown preset or a wrong option is a compile error.
useViraMask(form, {
// a preset name
tckn: "tckn",
// a preset with overrides
birthDate: { preset: "date", date: { max: "today", minAge: 18 } },
// React Hook Form rules pass through
phone: { preset: "phone", country: "TR", rules: { required: "required" } },
// nested field: quote the key or set name
"address.zip": { mask: "99999" },
zip2: { name: "address.zip2", mask: "99999" },
// write the masked text into the form
iban: { preset: "iban", country: "TR", output: "masked" },
// when the mask is full
card: { preset: "card", onComplete: () => form.setFocus("expiry") },
})rules:required,minLength,deps,setValueAs,disabled,onChangeand the otherregisterrules.country: the Turkish layout forphone,iban,currencyandnumber("TR"for now). The defaults are global.output:"raw"(default) or"masked".onComplete: runs once when the mask is full. You decide where focus goes.
Linked fields (from)
A mask can depend on another field. The link is explicit and typed: from must be a real field of the form.
cvv→card: 4 digits for Amex, 3 otherwise.currency→ a select: symbol and separators follow the currency.date→ a start date: the end can't be before it (date.beforeFrom).
Form value: 1234.56
Display: ₺1.234,56
When the linked field changes the mask is re-applied: if the card turns from Amex to Visa, the CVV is trimmed to 3 digits on screen and in the form.
Return value
Each key returns an object you spread on an input. Every key in it is a valid input prop; nothing unknown reaches the DOM.
name,ref,defaultValueonChange,onBlur,onCompositionEndtype,inputMode,autoCompletedisabled(when set in rules),aria-invalid(when there is an error)
The input is uncontrolled (defaultValue); don't pass value. Read the raw value with form.watch("phone").
React Hook Form compatibility
reset()andsetValue()update the input. Pass raw values (ISO for dates:2026-07-29).watch,getValues,formState,ControlleranduseFieldArraywork alongside it.- Mask validation is added as the
validate.maskrule; your ownvalidaterules are kept. - With a resolver (Zod, Valibot), wrap it with maskResolver.
- Formatting waits until IME composition (Chinese, Japanese, dead keys) ends.