Dice UI

Mask Input

An input component that formats user input with predefined patterns like phone numbers, dates, and credit cards.

API

Installation

pnpm dlx shadcn@latest add @diceui/mask-input

Layout

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 render prop 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:

PatternFormatExampleDescription
phone(###) ###-####(555) 123-4567US phone number
ssn###-##-####123-45-6789Social Security Number
date##/##/####12/25/2023Date (MM/DD/YYYY)
time##:##14:30Time (HH:MM)
creditCard#### #### #### ####1234 5678 9012 3456Credit card number
creditCardExpiry##/##12/25Credit card expiry date (MM/YY)
zipCode#####12345US ZIP code
zipCodeExtended#####-####12345-6789US ZIP+4 code
currencyDynamic$1,234.56Currency formatting using Intl.NumberFormat
percentage##.##%12.34%Percentage with decimals
licensePlate###-###ABC-123License plate format
ipv4###.###.###.###192.168.1.1IPv4 address
macAddress##:##:##:##:##:##00:1B:44:11:3A:B7MAC address
isbn###-#-###-#####-#978-0-123-45678-9ISBN-13 book identifier
ein##-#######12-3456789Employer 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.

PatternDescription
phoneUS phone number
ssnSocial Security Number
dateDate (MM/DD/YYYY)
timeTime (HH:MM)
creditCardCredit card number
creditCardExpiryCredit card expiry date (MM/YY)
zipCodeUS ZIP code
zipCodeExtendedUS ZIP+4 code
currencyCurrency formatting using Intl.NumberFormat
percentagePercentage with decimals
licensePlateLicense plate format
ipv4IPv4 address
macAddressMAC address
isbnISBN-13 book identifier
einEmployer 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 AttributeValue
[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

KeyDescription
TabMoves focus to or away from the input.
Shift + TabMoves focus to the previous focusable element.
BackspaceRemoves the previous character, intelligently handling mask characters.
DeleteRemoves the next character.
Ctrl + VCmd + VPastes content with intelligent mask formatting.
Ctrl + ACmd + ASelects all input content.

On this page