Mask Input
An input component that formats user input with predefined patterns like phone numbers, dates, and credit cards.
Installation
pnpm dlx shadcn@latest add @diceui/mask-inputLayout
Import and use the component directly.
import { MaskInput } from "@/components/ui/mask-input";
<MaskInput
mask="phone"
placeholder="Enter phone number"
maskPlaceholder="(___) ___-____"
onValueChange={(masked, unmasked) => {
console.log('Masked:', masked); // "(555) 123-4567"
console.log('Unmasked:', unmasked); // "5551234567"
}}
/>Features
- Smart cursor positioning - Cursor stays in the correct position during typing and pasting
- Paste support - Intelligently handles pasted content with proper formatting
- Built-in patterns - Common formats like phone, SSN, date, credit card, etc.
- Custom patterns - Create your own mask patterns with validation
- Optional mask placeholders - Control when mask format hints are shown with
maskPlaceholder - TypeScript support - Full type safety with IntelliSense
- Accessibility - ARIA attributes and keyboard navigation
- Form integration - Works seamlessly with form libraries
- Composition support - Use
renderprop to render as a different component
Examples
With custom patterns
Create custom mask patterns for specific formatting needs.
With validation modes
Control when validation occurs with different validation modes, similar to react-hook-form.
Card information
Card information with credit card number, expiry date, and CVC fields.
With form
Integrate masked inputs with form validation using react-hook-form.
Built-in Mask Patterns
The component includes several predefined mask patterns:
| Pattern | Format | Example | Description |
|---|---|---|---|
phone | (###) ###-#### | (555) 123-4567 | US phone number |
ssn | ###-##-#### | 123-45-6789 | Social Security Number |
date | ##/##/#### | 12/25/2023 | Date (MM/DD/YYYY) |
time | ##:## | 14:30 | Time (HH:MM) |
creditCard | #### #### #### #### | 1234 5678 9012 3456 | Credit card number |
creditCardExpiry | ##/## | 12/25 | Credit card expiry date (MM/YY) |
zipCode | ##### | 12345 | US ZIP code |
zipCodeExtended | #####-#### | 12345-6789 | US ZIP+4 code |
currency | Dynamic | $1,234.56 | Currency formatting using Intl.NumberFormat |
percentage | ##.##% | 12.34% | Percentage with decimals |
licensePlate | ###-### | ABC-123 | License plate format |
ipv4 | ###.###.###.### | 192.168.1.1 | IPv4 address |
macAddress | ##:##:##:##:##:## | 00:1B:44:11:3A:B7 | MAC address |
isbn | ###-#-###-#####-# | 978-0-123-45678-9 | ISBN-13 book identifier |
ein | ##-####### | 12-3456789 | Employer Identification Number |
Custom Mask Patterns
Create custom patterns using the MaskPattern interface:
const customPattern: MaskPattern = {
pattern: "###-###-####",
transform: (value, opts) => value.replace(/[^A-Z0-9]/gi, "").toUpperCase(),
validate: (value, opts) => value.length === 10,
};
<MaskInput
mask={customPattern}
placeholder="Enter license plate"
maskPlaceholder="ABC-1234"
/>Currency Formatting
The currency mask uses the Intl.NumberFormat API for localization and currency formatting.
// Default USD formatting
<MaskInput mask="currency" />
// Euro formatting with German locale
<MaskInput
mask="currency"
currency="EUR"
locale="de-DE"
/>
// Japanese Yen formatting
<MaskInput
mask="currency"
currency="JPY"
locale="ja-JP"
/>
// British Pound formatting
<MaskInput
mask="currency"
currency="GBP"
locale="en-GB"
/>Mask Placeholders
Use the maskPlaceholder prop to control when mask format hints are shown. The mask placeholder only appears when the input is focused and the prop is provided.
// Shows mask placeholder when focused
<MaskInput
mask="phone"
placeholder="Enter phone number"
maskPlaceholder="(___) ___-____"
/>
// No mask placeholder - just regular placeholder behavior
<MaskInput
mask="phone"
placeholder="Enter phone number"
/>API Reference
MaskInput
The main masked input component that handles formatting and user input.
Prop
Type
MaskPattern
Interface for creating custom mask patterns.
Prop
Type
MaskPatternKey
Predefined mask pattern keys for common input formats.
| Pattern | Description |
|---|---|
phone | US phone number |
ssn | Social Security Number |
date | Date (MM/DD/YYYY) |
time | Time (HH:MM) |
creditCard | Credit card number |
creditCardExpiry | Credit card expiry date (MM/YY) |
zipCode | US ZIP code |
zipCodeExtended | US ZIP+4 code |
currency | Currency formatting using Intl.NumberFormat |
percentage | Percentage with decimals |
licensePlate | License plate format |
ipv4 | IPv4 address |
macAddress | MAC address |
isbn | ISBN-13 book identifier |
ein | Employer Identification Number |
TransformOptions
Options passed to the transform function for advanced formatting.
Prop
Type
ValidateOptions
Options passed to the validate function for enhanced validation.
Prop
Type
Data Attributes
| Data Attribute | Value |
|---|---|
[data-disabled] | Present when the input is disabled. |
[data-invalid] | Present when the input has validation errors. |
[data-readonly] | Present when the input is read-only. |
[data-required] | Present when the input is required. |
Accessibility
Keyboard Interactions
| Key | Description |
|---|---|
| Tab | Moves focus to or away from the input. |
| Shift + Tab | Moves focus to the previous focusable element. |
| Backspace | Removes the previous character, intelligently handling mask characters. |
| Delete | Removes the next character. |
| Ctrl + VCmd + V | Pastes content with intelligent mask formatting. |
| Ctrl + ACmd + A | Selects all input content. |