Masked Input
React masked input that formats phones, dates, plates and IBANs as you type, keeps the caret steady on edits and pastes, and returns masked and raw values.
Installation
npx shadcn@latest add @minidev/masked-input
MiniDev UI is in the shadcn registry directory, so the short form works with no setup. Older CLI versions can use the full URL: npx shadcn@latest add https://ui.minidev.pro/r/masked-input.json
Usage
import { MaskedInput } from "@/components/ui/masked-input"
export function Example() {
return (
<MaskedInput />
)
}
Exports MaskedInput applyMask
API reference
| Prop | Type | Default |
|---|---|---|
| maskThe mask. When omitted, the `examples` list is rendered instead. | string | - |
| labelVisible label above the field. Without one, pass `aria-label`. | React.ReactNode | - |
| aria-labelAccessible name when there is no visible `label`. | string | - |
| valueRaw value (slot characters only), for a controlled field. | string | - |
| defaultValueInitial raw value when uncontrolled. Extra or mismatched characters are dropped. | string | "" |
| onChangeFires on every edit with `{ masked, raw, complete }`. | (value: MaskedValue) => void | - |
| guideShow the remaining mask while typing. `true` uses `guideChar`; a string is a template as long as the mask, e.g. "MM/DD/YYYY". | boolean | string | true |
| guideCharCharacter drawn for empty slots when `guide` is `true`. | string | "_" |
| uppercaseCapitalise letters as they are typed. | boolean | false |
| placeholderShown when empty and unfocused. Defaults to the guide template. | string | - |
| hintHelp text under the field, linked with aria-describedby. Turns danger-coloured when `invalid`. | React.ReactNode | - |
| showCompleteShow a check once every slot is filled. | boolean | true |
| showRawShow the raw value (slot characters only) under the field. | boolean | true |
| invalidMarks the field invalid (aria-invalid) and colours the hint. | boolean | false |
| disabled | boolean | false |
| required | boolean | false |
| id | string | - |
| nameForm field name. The submitted value is the masked string. | string | - |
| autoComplete | string | "off" |
| inputModeVirtual keyboard hint. Defaults to "numeric" for digit-only masks. | React.HTMLAttributes<HTMLInputElement>["inputMode"] | - |
| examplesFields rendered when no `mask` is passed. | MaskedInputExample[] | EXAMPLES |
| className | string | - |
When to use
Mask tokens: "9" digit, "A" letter, "*" letter or digit, "\\" escapes a token; anything else is a literal. The caret stays where you expect on insert, delete and paste, pasted text is refitted to the mask, and an optional guide shows the characters still to come. onChange returns the masked string, the raw characters and whether the mask is full. Use it for fixed-shape values (dates, plates, IBANs). For phone numbers with no fixed shape use PhoneInput; for one-time codes use OtpInput. Pass mask for a single field; without it the component renders examples.
Accessibility
- The guide text and the completion check are
aria-hidden, so completeness is not announced. Validate on submit and useinvalidwithhintfor errors. hintis linked witharia-describedby, andinvalidsetsaria-invalid. The raw value line starts with a visually hidden "Raw value".- Digit-only masks get
inputMode="numeric". Spell check and autocorrect are always off, anduppercasesetsautoCapitalize="characters".
Frequently asked questions
What does onChange give me?
An object: { masked, raw, complete }, for example { masked: "(415) 555-0142", raw: "4155550142", complete: true }. value and defaultValue take the raw characters, while a form submits the masked string under name.
What is the small grey text under my field?
The raw value. showRaw defaults to true; pass showRaw={false} to hide it. The green check when the mask is full is showComplete, also on by default.
Can I format a value outside the input?
Yes. The file exports applyMask: applyMask("(999) 999-9999", "5550102030") returns "(555) 010-2030". Use it to show stored values in tables or receipts.
Should I use it for international phone numbers?
No. A mask has one fixed shape. For numbers in any country's format use PhoneInput, and validate on submit.
Dependencies
npm
Built from
Examples
Date with guide
A string guide as long as the mask shows the characters still to come, in place of underscores.
Uppercase code
The star slot takes a letter or a digit, and uppercase capitalises letters as they are typed.
Controlled value
onChange gives the masked string, the raw characters and whether every slot is filled; pass the raw value back as value, and showComplete draws a check once the number is whole.
Invalid and disabled
invalid turns the border and hint red and hides the completion check; disabled locks the field.