Input Mask

Free

8 built-in mask presets (phone, credit card, date, time, SSN, ZIP, IP) plus custom patterns, with 3 visual variants.

</>sv-input-mask maskformatinput
import { SvInputMaskComponent } from '@styloviz/forms/input-mask';

Demo & usage

Basic usage

Usage example — open the gallery for the full interactive demo.

An interactive demo for this component lives in the gallery.

Open live demo

More examples

Date and time in the locale

The `date` preset's order and separator come from `Intl` for `locale` (default: the kit locale) — DD.MM.YYYY in German, YYYY/MM/DD in Japanese — and the calendar check follows the order. `time` takes the locale's separator. `phone-us`, `ssn` and `zip` are US formats and are labelled so.

example.htmlAngular
<sv-input-mask preset="date" locale="de" [(value)]="birthday" />

Custom pattern

Use customMask for formats outside the presets.

example.htmlAngular
<sv-input-mask preset="custom" customMask="AA-####" label="Asset ID" [(ngModel)]="assetId" />

Signal Forms

Bind it with `[formField]` from `@angular/forms/signals` — the control is a ControlValueAccessor, which Signal Forms drives on Angular 21 and 22 alike.

input-mask.signal-forms.tsTypeScript
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { FormField, form, minLength, required } from '@angular/forms/signals';
import { SvInputMaskComponent } from '@styloviz/forms/input-mask';

@Component({
  selector: 'app-input-mask-signal-forms',
  standalone: true,
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [FormField, SvInputMaskComponent],
  template: `
    <sv-input-mask [formField]="contact.phone" preset="phone-us" label="Phone number" />
    <p>Phone: {{ contact.phone().value() || '—' }} · valid: {{ contact.phone().valid() }}</p>
  `,
})
export class InputMaskSignalFormsExample {
  readonly model = signal({ phone: '' });
  readonly contact = form(this.model, (path) => {
    required(path.phone);
    // '(555) 010-9999' — the full mask is 14 characters.
    minLength(path.phone, 14);
  });
}

When to use

  • Enforcing a fixed input format: phone, credit card, date, SSN, ZIP, IP.
  • You want the display formatted while still capturing the raw value.
  • For validation without rigid formatting, a Text Field with `state` is enough.

Async states

None — this component has nothing to load, so it has no loading, empty or error state to drive.

Inputs

NameTypeDefaultDescription
localestringsvLocale()BCP 47 locale for the `date` and `time` presets: the date's field order and separator — `MM/DD/YYYY` in en-US, `DD.MM.YYYY` in German, `YYYY/MM/DD` in Japanese — and the time separator come from `Intl`, and the placeholder letters from the labels. `phone-us`, `ssn` and `zip` are US formats whatever the locale, and say so in their default labels.
presetInputMaskPreset'phone-us'Built-in mask preset (phone, credit card, date, SSN, ZIP, IP…).
customMaskstring''Custom mask pattern, used when `preset` is `custom`. Tokens: `#` a digit, `A` a letter, `*` either; every other character is a literal that is typed in for the user (e.g. '###-##' or 'AA-####'). A mask with no token in it can never accept input.
valuestring (two-way)''Formatted value (two-way).
sizeInputMaskSize'md'Field size: sm, md or lg.
variantInputMaskVariant'default'Visual variant.
disabledbooleanfalseDisable the field.
labelstring''Visible field label rendered above the input.
ariaLabelstring''Accessible name applied via `aria-label` on the actual `<input>` while `label` is hidden. Use when a parent component owns the visible label and only needs the input to announce its purpose (e.g. `sv-phone-input`, which renders a single shared label for the country button + number field).
errorstring''Error message shown below the field.
iconstring''Name of an icon in `@styloviz/icons`, e.g. `'credit-card'`. A name, not a raw `<path d="…">` string: the library supplies the sizing, stroke and dark-mode handling.
sensitivityInputMaskSensitivity'auto'Whether this field carries a regulated or personally identifying value.
placeholderstring''Placeholder override. Empty uses the mask's own underscores.
requiredbooleanfalseMark the field required (native `required` + `aria-required`).
namestring''The `name` the input submits under in a native form.
inputmode'tel' | 'numeric' | 'decimal' | 'text' | nullnullVirtual-keyboard override. `null` keeps the preset's choice (`tel` for the phone presets, `numeric` for the digit presets, `text` for `custom`) — a `custom` mask that only takes digits wants `'tel'` or `'numeric'` here, or a phone shows the full QWERTY keyboard.
autocompletestring''`autocomplete` token override, e.g. `'tel-national'`. Empty keeps the preset's token. Ignored on a sensitive field, which is always `off`.
inputDir'ltr' | 'rtl' | 'auto' | nullnullText direction of the input itself. `'ltr'` keeps a number's digits and separators in reading order inside a right-to-left page, where `(555) 010` would otherwise be laid out around its punctuation. `null` inherits.
errorMessagestring''Client-side validation message.
serverErrorSvServerErrornullA rejection reported by the server about this value. Self-expiring.

Outputs

EventPayload typeDescription
changedstringEmitted with the masked/formatted value.
rawChangedstringEmitted with the raw (unformatted) value.
valueChangestringEmits the new `value` whenever it changes — the output half of `[(value)]`.

Customization & theming

  • `inputmode`, `autocomplete` and `inputDir` override the preset's keyboard, autofill token and text direction — e.g. a digits-only `custom` mask wants `inputmode="numeric"`; a sensitive field stays `autocomplete="off"` whatever is passed.
  • Eight presets or a fully custom pattern; three visual variants and a leading `icon`.
  • Mask tokens: `#` a digit (any script — `\p{Nd}`, not `[0-9]`), `A` a letter, `*` either. Everything else in the pattern is a literal the user may type or skip.
  • `hasRejected()`, `rejectedChars()` and `maskComplete()` report what the mask could and could not hold, without deciding whether the value is right — that stays with the host.
  • Pasting is handled as a paste rather than as very fast typing: a marked country code (`+1 …`, `00 1 …`) is dropped when the mask is exactly full without it, so a number copied from a contact list arrives intact instead of shifted a place.
  • Emits both the raw (`rawChanged`) and formatted (`changed`) value; `error` shows a validation message.
  • Two-way bind `[(value)]` or a reactive control (CVA).
  • `locale` drives the `date` (field order + separator) and `time` (separator) presets; the value is the masked string in that order, so bind a locale-aware parser if you store an ISO date.

Forms integration

  • sv-input-mask — ControlValueAccessor.[formControl], formControlName, [(ngModel)] and Signal Forms [formField] all work.

Best practices

  • Do: Store the raw value (`rawChanged`) and show the masked value to users.
  • Do: Pick a `preset` when one exists rather than re-deriving a `customMask`.
  • Don't: Don't reach for a mask for a VARIABLE-width format. UK postcodes are `M1 1AA`, `SW1A 1AA`, `B33 8TH` and `DN55 1PT` — four shapes, and no single mask expresses them. Use a text field with validation instead. The mask tokens (`#` digit, `A` letter, `*` either) make more formats expressible; they do not make a fixed mask the right tool.
  • Do: Watch `hasRejected()` / `rejectedChars()`. Characters the mask cannot hold are dropped — that is what a mask does — and they are counted and announced rather than vanishing. Unreported, `SW1A 1AA` in a ZIP field would become `11` and `+1 (555) 010-9999` pasted into a US phone field `(155) 501-0999`, both silently, both looking complete and correct.
  • Don't: Don't read `maskComplete()` as valid. `(000) 000-0000` fills every slot and is not a phone number. Completeness is the shape; correctness is yours, and `error` is where your answer goes.
  • Tip: `phone-intl`'s mask assumes a ONE-DIGIT country code, which only +1 and +7 are. `+353 1 234 5678` has exactly eleven digits, so nothing overflows and nothing is reported — and the field still renders `+3 (531) 234-5678`. Use a dedicated Phone Input for international numbers.
  • Tip: Communicate the expected format via the label or hint for screen-reader users.
  • Don't: Don't read `preset="credit-card"` as compliance. Formatting a PAN does not encrypt or shield it — the digits are in a DOM node every script on your page can read, which is the difference between SAQ-A and SAQ-A-EP. SAQ-A-EP's scope includes your analytics, tag manager, chat widget and A/B tool. If you do not need to hold the number, use your processor's hosted field instead.
  • Do: Account for every session-replay script on the page. FullStory, Hotjar, Datadog RUM, Clarity, Smartlook and LogRocket record the DOM and read input values — "masked" is masked to the eye only. A sensitive preset sets every opt-out those tools honour, which is a best effort and not a guarantee about a script you chose to load.
  • Do: Use `maskedValue()` (`•••• •••• •••• 4242`) or `last4()` for confirmation screens, receipts, logs, analytics and support tooling. Both redact everything when the value has fewer than five digits, because four digits of a four-digit value is not a redaction.
  • Do: Set `sensitivity="sensitive"` on a custom mask that carries something regulated — a national ID, a policy number, a date of birth. `auto` only knows about the `credit-card` and `ssn` presets.
  • Don't: Don't let a generic "save draft" mechanism persist this field. It is the most common accidental persistence, because the draft code was written for a different form.

Accessibility

  • The `label` is associated with the input via `for`/`id`; `inputmode` adapts the on-screen keyboard (tel/numeric) to the preset.
  • When `error` is set the input gets `aria-invalid` and is linked to the message via `aria-describedby` (the message uses `role="alert"`).
  • Characters the mask could not hold are reported in a `role="status"` line linked by `aria-describedby` — a count, not "invalid input", because the field is showing a complete and plausible value that is not what was entered. It is not `aria-invalid`: a partial fit may be exactly what the host expects.
  • Implements ControlValueAccessor — works with `[(ngModel)]`, `[formControl]` and `formControlName`.

Feedback on Input Mask

Spotted a gap in this page, or something that didn't work as described? Comments are GitHub Discussions in the public styloviz/community repo, so posting needs a GitHub account.

Loads giscus.app and GitHub — nothing is fetched until you click.