# 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.

- Kind: Component (Forms)
- Docs: https://ui.minidev.pro/docs/masked-input
- Source (registry JSON with the full file): https://ui.minidev.pro/r/masked-input.json
- License: MIT. One file, yours to edit after install.

## 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`.

## Installation

### shadcn CLI (copies the source into your project)

```bash
npx shadcn@latest add @minidev/masked-input
```

Writes `components/ui/masked-input.tsx` plus the MiniDev files it is built from (input) and installs its npm dependencies. Older CLI versions: `npx shadcn@latest add https://ui.minidev.pro/r/masked-input.json`.

### npm package

```bash
npm i minidev-ui-kit lucide-react
```

Add the tokens to your global stylesheet once:

```css
@import "tailwindcss";
@import "minidev-ui-kit/styles.css";
@source "../node_modules/minidev-ui-kit";
```

Next.js also needs `transpilePackages: ["minidev-ui-kit"]` in next.config.

## Import

After the shadcn install:

```tsx
import { MaskedInput, applyMask } from "@/components/ui/masked-input"
```

From the npm package:

```tsx
import { MaskedInput } from "minidev-ui-kit/ui/masked-input"
```

## Usage

```tsx
import { MaskedInput } from "@/components/ui/masked-input"

export function Example() {
  return (
    <MaskedInput />
  )
}
```

## Props

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| `mask` | `string` | - | no | The mask. When omitted, the `examples` list is rendered instead. |
| `label` | `React.ReactNode` | - | no | Visible label above the field. Without one, pass `aria-label`. |
| `aria-label` | `string` | - | no | Accessible name when there is no visible `label`. |
| `value` | `string` | - | no | Raw value (slot characters only), for a controlled field. |
| `defaultValue` | `string` | `""` | no | Initial raw value when uncontrolled. Extra or mismatched characters are dropped. |
| `onChange` | `(value: MaskedValue) => void` | - | no | Fires on every edit with `{ masked, raw, complete }`. |
| `guide` | `boolean \| string` | `true` | no | Show the remaining mask while typing. `true` uses `guideChar`; a string is a template as long as the mask, e.g. "MM/DD/YYYY". |
| `guideChar` | `string` | `"_"` | no | Character drawn for empty slots when `guide` is `true`. |
| `uppercase` | `boolean` | `false` | no | Capitalise letters as they are typed. |
| `placeholder` | `string` | - | no | Shown when empty and unfocused. Defaults to the guide template. |
| `hint` | `React.ReactNode` | - | no | Help text under the field, linked with aria-describedby. Turns danger-coloured when `invalid`. |
| `showComplete` | `boolean` | `true` | no | Show a check once every slot is filled. |
| `showRaw` | `boolean` | `true` | no | Show the raw value (slot characters only) under the field. |
| `invalid` | `boolean` | `false` | no | Marks the field invalid (aria-invalid) and colours the hint. |
| `disabled` | `boolean` | `false` | no |  |
| `required` | `boolean` | `false` | no |  |
| `id` | `string` | - | no |  |
| `name` | `string` | - | no | Form field name. The submitted value is the masked string. |
| `autoComplete` | `string` | `"off"` | no |  |
| `inputMode` | `React.HTMLAttributes<HTMLInputElement>["inputMode"]` | - | no | Virtual keyboard hint. Defaults to "numeric" for digit-only masks. |
| `examples` | `MaskedInputExample[]` | `EXAMPLES` | no | Fields rendered when no `mask` is passed. |
| `className` | `string` | - | no |  |

## Examples

### Date with guide

A string guide as long as the mask shows the characters still to come, in place of underscores.

```tsx
<MaskedInput mask="99/99/9999" label="Due date" guide="MM/DD/YYYY" hint="INV-2041 is due 30 days after issue." />
```

### Uppercase code

The star slot takes a letter or a digit, and uppercase capitalises letters as they are typed.

```tsx
<MaskedInput mask="****-****" label="Promo code" guide="XXXX-XXXX" uppercase showRaw={false} hint="Letters and digits, case does not matter." />
```

### 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.

```tsx
function ControlledDemo() {
  const [card, setCard] = React.useState({ masked: "", raw: "", complete: false })
  return (
    <div className="flex w-full max-w-sm flex-col gap-3">
      <MaskedInput
        mask="9999 9999 9999 9999"
        label="Card number"
        value={card.raw}
        onChange={setCard}
        autoComplete="cc-number"
        showRaw={false}
        showComplete
        hint="Try 4242 4242 4242 4242"
      />
      <Button className="self-start" disabled={!card.complete}>
        Pay INV-2041
      </Button>
    </div>
  )
}
```

### Invalid and disabled

invalid turns the border and hint red and hides the completion check; disabled locks the field.

```tsx
<MaskedInput mask="99/99" label="Expiry" defaultValue="1329" invalid hint="That month does not exist." />
<MaskedInput mask="99-9999999" label="Tax ID" defaultValue="123456789" disabled hint="Locked after verification." />
```

## Accessibility

- The guide text and the completion check are `aria-hidden`, so completeness is not announced. Validate on submit and use `invalid` with `hint` for errors.
- `hint` is linked with `aria-describedby`, and `invalid` sets `aria-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, and `uppercase` sets `autoCapitalize="characters"`.

## FAQ

### 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`](https://ui.minidev.pro/docs/phone-input), and validate on submit.

## Rules for generating code

- Use semantic tokens only (bg, surface, sunken, border, fg, fg-muted, fg-subtle, ink, accent, success, warning, danger). Never raw hex or palette steps like blue-500.
- Shadows only from the set: shadow-xs, shadow-sm, shadow-raised, shadow-md, shadow-lg, shadow-overlay.
- Numbers use tabular-nums. Dark mode is the `dark` class on <html>; components never branch on theme.

## Dependencies

- npm: `lucide-react`
- Built from: [input](https://ui.minidev.pro/docs/input/md)

## Related

- [Phone Input](https://ui.minidev.pro/docs/phone-input/md): A telephone number field with type tel, a numeric keypad on mobile, tel autocomplete, tabular digits and a default aria label. Styled like the React base input.
- [Otp Input](https://ui.minidev.pro/docs/otp-input/md): A React one time code input where one real input powers the slots, so paste, SMS autofill and screen readers work natively. Has groups, error shake and success.
- [Floating Label Input](https://ui.minidev.pro/docs/floating-label-input/md): React floating label input and textarea whose label springs onto the top border on focus or fill, with helper text, an error state and a leading icon.
- [Autocomplete](https://ui.minidev.pro/docs/autocomplete/md): Text input that filters string options as you type, using ARIA combobox and listbox roles. Works controlled or uncontrolled and lists up to eight matches.
- [Calendar Month](https://ui.minidev.pro/docs/calendar-month/md): Month calendar grid with weekday headers and clickable day cells that highlight the selected date. A simple Tailwind base for date views and schedulers.
- [Character Count](https://ui.minidev.pro/docs/character-count/md): Live React character counter that shows the current count against a limit and turns red when it goes over. Announces changes politely to screen readers.

---

All 557 components: https://ui.minidev.pro/llms.txt (full text: https://ui.minidev.pro/llms-full.txt).
