Input mask

A text field that formats what is typed into a fixed pattern: phone numbers, IBANs, plates, dates typed as digits. The caret stays where the user expects with every keyboard.

  • vig-input-mask
Import
import { InputMask, MASK_DIGIT, MASK_LETTER, MASK_ALPHANUMERIC } from '@vigdx/ui/input-mask';

Basic

Pass a mask pattern and bind [(value)] . Literals appear as the user types, Backspace skips over them, and the value holds the typed characters only ( 5551234567 ). In a pattern 9 is a digit, a a letter, * either, and \ makes the next character literal. Escape token characters you mean literally: in +90 (999) the 9 is a slot, so write +\90 (999) . The keyboard follows the mask: digits only open the digit pad.

Value: ""

Placeholder character, formatted value and completion

placeholderChar="_" shows the whole pattern while the field is focused or filled. valueFormat="formatted" makes the value the text as shown ( 31/12/19 ) instead of the raw characters. An incomplete value is kept, never cleared: validate it with the form. (completed) fires when the last slot is filled.

DD/MM/YYYY

Date: "" · Plate: ""

Custom slots

For anything a pattern cannot say, pass an array of literal strings and MaskSlot objects: { accepts: (char) => boolean, transform?, inputMode? } . MASK_DIGIT , MASK_LETTER and MASK_ALPHANUMERIC are the pattern's slots. Here letters are upper-cased as they are typed.

Value: ""

Icons, label position, states and sizes

The vigInputPrefix and vigInputSuffix slots of vig-input work here too. labelPosition , hideLabel , disabled , readonly and size behave as in the other text fields.

Signal Forms

vig-input-mask implements FormValueControl<string> for [formField] . With the raw value, a complete phone number is 10 characters long, so minLength checks completeness. A value set from outside may be raw or formatted ( +1 (555) 123-4567 is read through its literals); it is written back in the field's own form, which leaves Reactive Forms and ngModel controls pristine but makes a Signal Forms field dirty.

Valid: no

Reactive Forms

Import VigValueAccessor next to the component to use formControlName , [formControl] or ngModel .

Value: "34000"

How editing works

  • Every edit is read from the beforeinput event, applied to the mask and written back with the caret in place, so nothing jumps in Chromium, Firefox or WebKit. Edits a browser does not let the field cancel (some Android keyboards, word deletions, cut, spell-check and autofill) are read back from the input event instead.
  • Typing in the middle moves the following characters along; deleting moves them back. Backspace and Delete skip over literals to the nearest character. A full mask refuses more characters instead of dropping the last one.
  • Paste and drop take formatted text ((555) 123-4567) or plain characters, and drop whatever overflows. Autofill fills the whole value.
  • IME compositions (Japanese, Chinese, Korean keyboards; Android word suggestions) are left alone until they end, then applied; full-width and Arabic-Indic digits become ASCII.
  • Undo and redo (Ctrl+Z, Ctrl+Shift+Z or Ctrl+Y; ⌘Z and ⇧⌘Z on Apple devices, on any keyboard layout) step through the field's own history, and fire (completed) when they bring back a complete value. The browser's Edit menu and shake to undo do not reach it: the field rewrites every edit, which the browser's own undo history does not record.
  • In masks that mix letters and digits, an edit that would move characters into slots that reject them is refused: typing 0 before 34A in 99 aaa 999 does nothing, and deleting a letter of aa99 selects it instead, so typing replaces it. Nothing after the caret is ever lost; select text to type over it.
  • A character the mask rejects, typed or pasted over a selection, leaves the selection as it is. Cut and drag remove exactly what they select, literals included, without taking a character next to them.

Styling hooks

Internal elements carry a stable data-part and the component's data-scope; both are public API, so removing one is a breaking change. Select a part by both, e.g. [data-scope="input-mask"][data-part="field"] { … }: the pair stays within this component when kit components nest, and also reaches parts in overlay panels, which render outside the host. State is exposed through aria-* and, where listed, a data-state; literal values are checked by the API gate, values computed from the component's state follow its documented types. Other data-* attributes listed below (such as data-opening while an overlay animates) are API too. See Customizing.

Scope: [data-scope="input-mask"]

InputMask styling hooks
Part Element data-state
field Positioned wrapper of the label, slots and control. —
label The visible (or floating) label. —
prefix Wrapper of the [vigInputPrefix] slot. —
input The native <input> (always dir="ltr" ). —
suffix Wrapper of the [vigInputSuffix] slot. —
hint The vig-hint (a display: contents host; style its text part). —
disabled-reason The vig-hint with the disabled reason ( display: contents ). —
error The vig-field-error (a display: contents host; style its message part). —

Accessibility

  • A native <input type="text"> named by a real <label>; hint, disabled reason and error are linked through aria-describedby. Describe the expected format in the hint or a placeholder example: screen readers read the value, not the pattern.
  • inputmode follows the mask, and autocorrect, auto-capitalization and spell check are off. Set autocomplete (tel-national, postal-code, cc-number…) so browsers can fill the field (WCAG 1.3.5).
  • The text always runs left to right (dir="ltr"), also on RTL pages, so phone numbers and codes keep their order; it aligns to the page's start, where the label and prefix sit.

API

InputMask

vig-input-mask component

A text field that formats what is typed into a fixed pattern — phone numbers, IBANs, codes, dates typed as digits:

Example :
<vig-input-mask label="Phone" mask="(999) 999-9999" autocomplete="tel-national" [(value)]="phone" />

In a pattern 9 is a digit, a a letter, * either and \ escapes a literal; pass an array of literals and MaskSlots for custom slots. The value is the typed characters only (5551234567); valueFormat="formatted" makes it the text as shown. An incomplete value is kept, never cleared: validate it with the form ((completed) fires once every slot is filled).

Editing goes through beforeinput (cancelled and applied by the field) and falls back to reading the input event's change, so mobile keyboards, IME compositions, paste, drop, autofill and undo all keep the caret in place. The text always runs left to right (dir="ltr"), also on RTL pages.

InputMask props
Name Type Default Description
autocompletestring | undefinedundefined

Native autocomplete, e.g. tel-national or postal-code, so autofill can help.

dirtybooleanfalse
—
disabledbooleanfalse
—
disabledReasonsreadonly WithOptionalFieldTree<DisabledReason>[][]
—
errorMessagesRecord<string, string>—
—
errorsreadonly ValidationError.WithOptionalFieldTree[][]
—
hiddenbooleanfalse
—
hideLabelbooleanfalse

Visually hides the label, which still names the field; pair it with a placeholder.

hintstring | undefinedundefined
—
invalidbooleanfalse
—
label required string—
—
labelPositionLabelPosition'float'

float (default) rests the label inside the field; top sets it above.

mask required Mask—

The pattern ('(999) 999-9999') or an array of literals and MaskSlots.

namestring''
—
pendingbooleanfalse
—
placeholderstring | undefinedundefined

Native placeholder of the empty field, e.g. an example value.

placeholderCharstring | undefinedundefined

Shows the whole pattern while the field is focused or filled, with this character in the empty slots (e.g. _). Unset, the text grows as the user types.

readonlybooleanfalse
—
requiredbooleanfalse
—
sizeFieldSize'md'
—
touchedbooleanfalse
—
value model string''
—
valueFormatInputMaskValueFormat'raw'

raw (default): the value holds the typed characters only (5551234567). formatted: the text as shown, up to the last typed character ((555) 123).

VigValueAccessor

directive

Bridges the vigdx form fields to Reactive Forms (formControl, formControlName) and template-driven forms (ngModel). Import it next to the component; Signal Forms ([formField]) need no accessor.

VigValueAccessor methods
Signature Description
registerOnChange(fn: (value: unknown) => void): void
—
registerOnTouched(fn: () => void): void
—
setDisabledState(disabled: boolean): void
—
writeValue(value: unknown): void
—

Are you sure?