fn

useMaskedInput

v0.0.14testeddemo

Headless input masking. Returns a bind object to spread onto an <input>/<textarea> (<input v-bind="bind">) — it carries the template ref and the event handlers, so there is no separate ref wiring. Conforms the value on every keystroke (insert/delete/paste/IME) with a correct caret, and exposes the masked/unmasked views plus a complete signal.

Examples

ts
const phone = useMaskedInput({ mask: '+1 (###) ###-####' });
// <input v-bind="phone.bind">
// phone.unmasked.value → '1234567890'
ts
const amount = useMaskedInput({
  mask: maskNumberOptions({ thousandSeparator: ',', precision: 2, prefix: '$' }),
  onAccept: ({ unmasked }) => save(unmasked),
});

Demo

Loading demo…

Signature

ts
export function useMaskedInput(options: UseMaskedInputOptions): UseMaskedInputReturn{ ... }

Parameters

ParameterTypeDescription
optionsUseMaskedInputOptionsThe mask and behavior

Returns

UseMaskedInputReturnbind, masked, unmasked, complete, ensureFitsMask, setValue
PropertyTypeDescription
maskedShallowRef<string>The conformed, displayed value (kept in sync with the element).
unmaskedReadonly<ShallowRef<string>>The raw value with fixed mask characters (and preset separators) stripped.
completeComputedRef<boolean>Whether masked fully satisfies the mask.
bindMaskInputBindingsSpread onto the input element: <input v-bind="bind">.
ensureFitsMask() => voidRe-conform the element's current value (e.g. after a programmatic set).
setValue(value: string) => voidSet the value programmatically — treated as raw input and conformed.