Helpers
Pure mask, validator and money functions you can use outside the hook.
All of them are pure and React-free. On the server, import them from @virastack/mask/server.
createMask
The same object the hook uses. format for masked display in lists, tables and reports; parse for text from outside.
import { createMask } from "@virastack/mask"
const phone = createMask({ preset: "phone", country: "TR" })
phone.format("5551234567") // "(555) 123 45 67"
phone.parse("+90 555 123 45 67") // "5551234567"
phone.validate("555") // "phone.incomplete"
phone.isComplete("5551234567") // true
const price = createMask("currency")
price.format("1234.5", { from: "USD" }) // "$1,234.5"getMaskPlaceholder
Builds placeholder text from a mask (2.4+). It takes what createMask takes. Every character to type becomes _; separators and {fixed} parts stay as they are, and optional [...] parts are left out. Masks that pick their pattern at runtime (card, cvv, plate) are read as they are for an empty field. Number, currency and date masks return undefined, and so does a mask with at most one required character, such as fullName. The hook does not set the placeholder itself; you pass it.
import { getMaskPlaceholder } from "@virastack/mask"
getMaskPlaceholder({ preset: "phone", country: "TR" }) // "(___) ___ __ __"
getMaskPlaceholder({ preset: "iban", country: "TR" }) // "TR__ ____ ____ ____ ____ ____ __"
getMaskPlaceholder("card") // "____ ____ ____ ____"
getMaskPlaceholder({ mask: "aa-9999[9]" }) // "__-____"
getMaskPlaceholder("currency") // undefined
const masks = { phone: { preset: "phone", country: "TR" } } as const
const { phone } = useViraMask(form, masks)
return <input {...phone} placeholder={getMaskPlaceholder(masks.phone)} />Validators
import {
getCardType,
isValidCard,
isValidIBAN,
isValidLuhn,
isValidPlate,
isValidTCKN,
isValidVKN,
} from "@virastack/mask"
getCardType("3782 822463 10005") // "amex"
isValidCard("4111111111111111") // true: length + Luhn
isValidTCKN("10000000146") // true
isValidVKN("1234567890") // true
isValidIBAN("DE89 3704 0044 0532 0130 00") // true, any country
isValidIBAN("TR330006100519786457841326") // true
isValidIBAN("TR33000610051978645784132") // false: a TR IBAN has 26 characters
isValidPlate("34 ABC 123") // true: province 01–81 and the official groups
isValidPlate("34 AB 12") // false: 2 letters need 3–4 digitsisValidPlate and the plate preset follow article 34 of the Araçların Satış, Devir ve Tescil Hizmetlerinin Yürütülmesi Hakkında Yönetmelik, Turkey's vehicle registration regulation: province code 01–81, then 1 letter and 4–5 digits, 2 letters and 3–4 digits, or 3 letters and 2–3 digits. The regulation excludes no Latin letter, so Q, W and X pass; Turkish letters don't. Name and trade-name plates (article 37) and special plates (article 36) are not checked.
To find the bank behind a Turkish IBAN, use getTurkishBank. For valid TCKNs and IBANs in tests and seed data, see test data.
Money
toMinorUnits turns a decimal string into integer minor units. The second argument is the number of fraction digits or an ISO currency code.
import { toMinorUnits } from "@virastack/mask"
toMinorUnits("1234.56", "TRY") // 123456
toMinorUnits("0.1", 2) // 10 (no float errors)
toMinorUnits("1500", "JPY") // 1500