Masked Input Examples
The examples below assume that you have imported the component and set up the theme.
Basic
Format a phone number while the user types, with a visible label and a format hint. mask sets the format, and aria-describedby links the hint to the field.
Type a letter to see the refused state.
<form class="masked-input-basic"> <label for="masked-input-phone">Phone</label> <sv-masked-input id="masked-input-phone" aria-describedby="masked-input-phone-hint" autocomplete="tel-national" mask="(999) 999-9999" name="phone" ></sv-masked-input> <small id="masked-input-phone-hint">Format: (555) 123-4567</small></form>
<script type="application/x-typescript" module> const form = document.querySelector<HTMLFormElement>(".masked-input-basic")!;
form.addEventListener("submit", event => event.preventDefault());</script>
<style> .masked-input-basic { display: grid; gap: 0.5em; place-items: center;
label { font-weight: 600; }
small { color: var(--sv-gray-3); } }</style>Date
Ask for a date, and show what each position means. A slot-char longer than one character gives a hint for each mask position, and pattern refuses a day past 31 or a month past 12 at validation.
<div class="masked-input-date"> <label for="masked-input-birth-date">Birth date</label> <sv-masked-input id="masked-input-birth-date" aria-describedby="masked-input-birth-date-hint" mask="99/99/9999" name="birth-date" pattern="(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[0-2])/[0-9]{4}" slot-char="DD/MM/YYYY" ></sv-masked-input> <small id="masked-input-birth-date-hint">Format: DD/MM/YYYY</small></div>
<style> .masked-input-date { display: grid; gap: 0.5em; place-items: center;
label { font-weight: 600; }
small { color: var(--sv-gray-3); } }</style>Hex Color
Accept a hex color and preview it when it is complete. The tokens property adds an h token for one hex digit, \# makes the # a literal, and completed tells when to draw the preview.
<div class="masked-input-hex-color"> <label for="masked-input-brand-color">Brand color</label> <sv-masked-input id="masked-input-brand-color" aria-describedby="masked-input-brand-color-hint" mask="\#hhhhhh" > <span class="preview" aria-hidden="true" slot="after"></span> </sv-masked-input> <small id="masked-input-brand-color-hint">Format: #1a2b3c</small></div>
<script type="application/x-typescript" module> import type { MaskedInput } from "@staticview/ui/masked-input";
const colorField = document.querySelector<MaskedInput>( ".masked-input-hex-color sv-masked-input" )!; const preview = document.querySelector<HTMLSpanElement>(".masked-input-hex-color .preview")!;
colorField.tokens = { h: /[0-9a-fA-F]/ };
colorField.addEventListener("valuechange", () => { preview.style.backgroundColor = colorField.completed ? colorField.value : ""; });</script>
<style> .masked-input-hex-color { display: grid; gap: 0.5em; place-items: center;
label { font-weight: 600; }
small { color: var(--sv-gray-3); }
.preview { inline-size: 1.25em; block-size: 1.25em; margin-inline-end: 0.5em; border: 1px solid var(--sv-gray-4); border-radius: var(--sv-bdr-rad-sm); } }</style>Promo Code
Accept a code in uppercase, however the user types it. The transform property changes each inserted character before the A token tests it.
<div class="masked-input-promo-code"> <label for="masked-input-promo-code-field">Promo code</label> <sv-masked-input id="masked-input-promo-code-field" aria-describedby="masked-input-promo-code-hint" autocomplete="off" mask="AAAA-9999" name="promo-code" ></sv-masked-input> <small id="masked-input-promo-code-hint">Format: SAVE-2026</small></div>
<script type="application/x-typescript" module> import type { MaskedInput } from "@staticview/ui/masked-input";
const promoCodeField = document.querySelector<MaskedInput>( ".masked-input-promo-code sv-masked-input" )!;
promoCodeField.transform = character => character.toUpperCase();</script>
<style> .masked-input-promo-code { display: grid; gap: 0.5em; place-items: center;
label { font-weight: 600; }
small { color: var(--sv-gray-3); } }</style>Time
Accept a 24-hour time, with a different rule for each digit. The mask property takes an array where a RegExp tests one character and a string is a literal, and pattern refuses an hour past 23 at validation.
<div class="masked-input-time"> <label for="masked-input-start-time">Start time</label> <sv-masked-input id="masked-input-start-time" aria-describedby="masked-input-start-time-hint" inputmode="numeric" name="start-time" pattern="([01][0-9]|2[0-3]):[0-5][0-9]" ></sv-masked-input> <small id="masked-input-start-time-hint">Format: 23:59</small></div>
<script type="application/x-typescript" module> import type { MaskedInput } from "@staticview/ui/masked-input";
const timeField = document.querySelector<MaskedInput>(".masked-input-time sv-masked-input")!;
timeField.mask = [/[0-2]/, /[0-9]/, ":", /[0-5]/, /[0-9]/];</script>
<style> .masked-input-time { display: grid; gap: 0.5em; place-items: center;
label { font-weight: 600; }
small { color: var(--sv-gray-3); } }</style>Card Number
Change the format while the user types, for card types with different groups. The modify property gets the raw value on each edit and returns the American Express mask, or undefined for the mask attribute.
Type or paste a number that starts with 34 or 37 to see the groups change.
<div class="masked-input-card-number"> <label for="masked-input-card-number-field">Card number</label> <sv-masked-input id="masked-input-card-number-field" aria-describedby="masked-input-card-number-hint" autocomplete="cc-number" mask="9999 9999 9999 9999" name="card-number" ></sv-masked-input> <small id="masked-input-card-number-hint">15 or 16 digits</small></div>
<script type="application/x-typescript" module> import type { MaskedInput } from "@staticview/ui/masked-input";
const cardNumberField = document.querySelector<MaskedInput>( ".masked-input-card-number sv-masked-input" )!;
cardNumberField.modify = rawValue => (/^3[47]/.test(rawValue) ? "9999 999999 99999" : undefined);</script>
<style> .masked-input-card-number { display: grid; gap: 0.5em; place-items: center;
label { font-weight: 600; }
small { color: var(--sv-gray-3); } }</style>Optional Extension
Accept a phone number with an optional extension. In mask, the tokens after ? are optional, and the field is valid once the phone number before them is filled.
<div class="masked-input-optional-extension"> <label for="masked-input-office-phone">Office phone</label> <sv-masked-input id="masked-input-office-phone" aria-describedby="masked-input-office-phone-hint" autocomplete="tel-national" mask="(999) 999-9999? x99999" name="office-phone" ></sv-masked-input> <small id="masked-input-office-phone-hint"> Format: (555) 123-4567 x12345. The extension is optional. </small></div>
<style> .masked-input-optional-extension { display: grid; gap: 0.5em; place-items: center;
label { font-weight: 600; }
small { color: var(--sv-gray-3); } }</style>