Input With Mask
An input with mask is a <div> wrapper around a text <input> and a decorative format-mask display (e.g., (___) ___-____) that overlays the input to show the user where to type each character.
Use it for structured fields such as phone numbers, dates, or document identifiers that have a predictable character pattern.
Implementation Notes
- Renders a wrapping
<div>with classinput-with-maskanddata-maskattribute labelprop is required — it setsaria-labelon the<input>maskprop is required — the format string displayed to the user- The mask display renders as
<span class="input-with-mask-display" aria-hidden="true"> - The actual input renders as
<input class="input-with-mask-control" type="text"> - The mask display is decorative; the input's
aria-labelis the accessible name value,placeholder,disabledare forwarded to the inputonInput/oninputcallback fires on input change
Props
label: string (required) -- aria-label for the inputmask: string (required) -- format mask (e.g.,(___) ___-____)value: string (default: "") -- input valueplaceholder: string (optional) -- input placeholderdisabled: boolean (default: false) -- disabled stateonInput/oninput: callback (optional) -- input change handler...restProps: any additional HTML attributes
Usage
US phone number mask:
<InputWithMask
label="Phone number"
mask="(___) ___-____"
value={phone}
oninput={(e) => phone = e.target.value}
/>
Date mask:
<InputWithMask
label="Date of birth"
mask="MM/DD/YYYY"
value={dob}
/>
Keyboard Interactions
- Tab: focus the input
- All standard text-input keyboard handling is delegated to the native
<input>
ARIA
- The input has the required
aria-labelfrom thelabelprop - The mask display has
aria-hidden="true"so screen readers do not announce the placeholder pattern - The wrapping
<div>has adata-maskattribute for consumer styling
When to Use
- Use for phone numbers, social security numbers, dates, and similar structured fields.
- Use when the format is predictable and showing the pattern helps users enter values correctly.
When Not to Use
- Do not use for free-form text — use a plain
TextInputinstead. - Do not use for sensitive masked content (passwords) — use
PasswordInput. - Do not use as a substitute for proper input types (
<input type="tel">,<input type="date">) when those are appropriate.
Headless
Renders the structural skeleton — <div> with <span> mask display and <input> control. The visual overlay technique that aligns the mask with the input characters is entirely the consumer's responsibility.
Styles
Consumer CSS targets the input-with-mask class plus the inner input-with-mask-display and input-with-mask-control classes. Typical treatments use absolute positioning to overlay the mask on the input, with monospace typography so the characters align.
Testing
- Verify the component renders a
<div>with classinput-with-mask - Verify the
data-maskattribute equals themaskprop - Verify the mask display renders inside
<span class="input-with-mask-display" aria-hidden="true"> - Verify the mask display text equals the
maskprop - Verify the input has class
input-with-mask-control - Verify the input has type
text - Verify the input has
aria-labelequal to thelabelprop - Verify the input value reflects the
valueprop - Verify the input is disabled when
disabledis true - Verify the input handler fires on input
Advice
- Designers: Use a monospace font for the input so the mask and the typed characters align. Choose a low-contrast color for the mask so it does not compete with the user's input.
- Developers: The mask display is purely visual. The input value remains a plain string — do not parse the mask characters into the value. Use a separate validation component (e.g.,
ValidationList) to verify the format.
Related components
text-input— a single-line text input field <input type="text">pin-input-div— a series of single-digit inputs for entering a PIN or OTP codeinput— a generic HTML input element
References
- US Web Design System Input Mask: https://designsystem.digital.gov/components/input-mask/
- HTML
<input type="text">: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/text - WAI-ARIA aria-hidden: https://www.w3.org/TR/wai-aria-1.2/#aria-hidden
- WCAG 1.3.5 Identify Input Purpose: https://www.w3.org/WAI/WCAG22/Understanding/identify-input-purpose