---
title: "Masked Input"
description: "A text field that formats the value as you type: card numbers, IBANs, phones, amounts, dates, or your own pattern. A guide shows what is left to type."
url: https://gelatinui.com/components/masked-input
markdown_url: https://gelatinui.com/components/masked-input.md
access: free
install: "npx shadcn@latest add @gelatin-ui/masked-input"
registry_item: https://gelatinui.com/r/masked-input.json
version: "2026-11-09"
index: https://gelatinui.com/llms.txt
docs_index: https://gelatinui.com/docs/llms.txt
---

# Masked Input

> A text field that formats the value as you type: card numbers, IBANs, phones, amounts, dates, or your own pattern. A guide shows what is left to type.

Page: https://gelatinui.com/components/masked-input

## When to use it

Best for: A text field that formats as people type, from a preset such as a card number or a date.

- Use it when a value has a fixed format.
- Use a preset from `lib/masks`: `pattern`, `currency`, `percent`, `date`, `time`, `cardNumber`, `iban`, or `hexColor`.
- Use `MaskedInputClear` for a button that empties the field.
- Use `rawName` to also submit the raw value with a form.

## When not to use it

- For a phone number with a country, use a [Phone Field](https://gelatinui.com/components/phone-field.md).
- For a one-time code, use an [Input OTP](https://gelatinui.com/components/input-otp.md).
- For free text, use an [Input](https://gelatinui.com/components/input.md).
- For a date with a calendar, use a [Date Picker](https://gelatinui.com/components/date-picker.md).

## Notes for agents

- Build the preset once at module level. A new preset object rebuilds the mask.
- `value` is the shown value. `onValueChange` gets the shown value, the raw value, and the checks.
- Use the preset checks for the error: `validate={(v) => preset.valid(v) ? null : "Enter a card number."}`.
- Do not share a phone mask between inputs. Build one per input.
- `MaskedInputClear` works only inside `MaskedInput`. Its default name is "Clear", so translate it.
- Use `MaskedInputControl` inside your own `InputGroup`.
- Put it in a `Field` for the label, the description, and the error.
- `onReject` runs when the mask rejects a key. Set `rejectFeedback={false}` to stop the 2px nudge.

## Anatomy

```text
MaskedInput
├─ MaskedInputControl (0..n)
└─ MaskedInputClear (0..n)
```

## API reference

### MaskedInputControl

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `className` | `string` |  |  |
| `rounded` | `"default" \| "full"` |  |  |
| `size` | `"default" \| "lg" \| "sm" \| "xs"` |  |  |
| `mask` | `MaskPreset<Raw>` |  | A preset from `#/lib/masks` (or `#/lib/phone-model`). Build it once: a new object rebuilds the mask. |
| `value` | `string` |  | The shown value (controlled). It is formatted by the mask, so "4155550132" shows formatted. |
| `defaultValue` | `string` |  | The first shown value (uncontrolled), formatted by the mask. |
| `onValueChange` | `function` |  | Runs on every change with the shown value, the raw value, and the checks. |
| `guide` | `boolean` | true | The ghost guide of the slots still to fill, while the input has focus. Default `true`. |
| `rawName` | `string` |  | Also submit the raw value under this name (a hidden input), next to the shown one. |
| `rejectFeedback` | `boolean` | true | Nudge the field 2px when the mask rejects a key. Default `true`. |
| `onReject` | `function` |  | Runs when the mask rejects a key (a letter in a number, a 13th month). |
| `ref` | `Ref<HTMLInputElement>` |  |  |

### MaskedInput

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `className` | `string` |  |  |
| `rounded` | `"default" \| "full"` |  |  |
| `size` | `"default" \| "lg" \| "sm" \| "xs"` |  |  |
| `mask` | `MaskPreset<Raw>` |  | A preset from `#/lib/masks` (or `#/lib/phone-model`). Build it once: a new object rebuilds the mask. |
| `value` | `string` |  | The shown value (controlled). It is formatted by the mask, so "4155550132" shows formatted. |
| `defaultValue` | `string` | "" | The first shown value (uncontrolled), formatted by the mask. |
| `onValueChange` | `function` |  | Runs on every change with the shown value, the raw value, and the checks. |
| `guide` | `boolean` |  | The ghost guide of the slots still to fill, while the input has focus. Default `true`. |
| `rawName` | `string` |  | Also submit the raw value under this name (a hidden input), next to the shown one. |
| `rejectFeedback` | `boolean` |  | Nudge the field 2px when the mask rejects a key. Default `true`. |
| `onReject` | `function` |  | Runs when the mask rejects a key (a letter in a number, a 13th month). |
| `ref` | `Ref<HTMLInputElement>` |  |  |
| `addons` | `boolean` | true | Show the start and end text of the preset (a currency symbol, "%", a unit). Default `true`. |

### MaskedInputClear

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `className` | `string` |  |  |
| `size` | `"icon-sm" \| "icon-xs" \| "sm" \| "xs"` |  |  |
| `rounded` | `"default" \| "full"` |  |  |
| `variant` | `"dashed" \| "default" \| "destructive" \| "destructive-outline" \| "ghost" \| "link" \| "media" \| "outline" \| "secondary"` |  |  |
| `onClick` | `InputGroupButtonProps["onClick"]` |  |  |

Also takes every prop of Base UI `Button`: https://base-ui.com/react/components/button

## Styling

| Part | Slot | States | CSS variables | Tokens |
| --- | --- | --- | --- | --- |
| MaskedInputControl | `masked-input-control` |  |  |  |
| MaskedInput | `masked-input` |  |  |  |
| MaskedInputClear | `masked-input-clear` | `data-hidden` |  | `motion-timing` |

## Usage

Build the preset once at module level, so the mask does not rebuild, and add `MaskedInputClear` as a child at the end.

The sample also uses `field`, which this add does not write. One command adds them with this item:

```bash
npx shadcn@latest add @gelatin-ui/masked-input @gelatin-ui/field
```

```tsx title="card-number.tsx"
import { Field, FieldLabel } from "@/components/ui/field";
import { MaskedInput, MaskedInputClear } from "@/components/ui/masked-input";
import { cardNumber } from "@/lib/masks";

const CARD = cardNumber();

export function CardNumber() {
  return (
    <Field name="card">
      <FieldLabel>Card number</FieldLabel>
      <MaskedInput mask={CARD} placeholder="1234 5678 9012 3456">
        <MaskedInputClear />
      </MaskedInput>
    </Field>
  );
}
```

## Compose

Rules:

- Compose the parts (`compose-parts`): Build each item from its named parts as children, and pass your text and icons in as parts.
- Every state is designed (`every-state`): Give each view of data its loading, empty, and error states: a skeleton in the final layout, an `Empty` with an illustration and one next action, and an error that says what failed and how to retry.

## Add it

```bash
npx shadcn@latest add @gelatin-ui/masked-input
```

It adds 26 registry items: 23 files, 5219 lines, and the npm packages `@base-ui/react`, `@mantine/hooks`, `@maskito/core`, `@maskito/kit`, `@maskito/react`, `@phosphor-icons/react`, `clsx`, `motion`, `tailwind-merge`, `tailwind-variants`.

## Built on

- [Input Group](https://gelatinui.com/components/input-group.md)

## For AI agents

- Every item, one file: https://gelatinui.com/llms.txt
- MCP server (`search_items`, `get_item`, `get_install_plan`): https://gelatinui.com/mcp

## Related

- [Input Group](https://gelatinui.com/components/input-group.md)
- [Phone Field](https://gelatinui.com/components/phone-field.md)
- [Field](https://gelatinui.com/components/field.md)

## Keyboard

| Keys | Action |
| --- | --- |
| Enter, Space | On `MaskedInputClear`: clears the field and puts focus back in it. |

## Accessibility

It is a native input, so a `Field` labels it and links its help and error text.

Each preset sets the keyboard and the autofill field.

`MaskedInputClear` has the name "Clear" by default. Translate it in another language.

## Motion

| Part | Motion |
| --- | --- |
| Clear button | fade · 150ms · ease-out (0, 0, 0.2, 1) · fades in when the field has text |
