React Text Mask

5.5.0 · active · verified Sun Apr 19

This package provides a React component, `<MaskedInput>`, for creating input fields that enforce specific formatting patterns on user input. It is part of the larger `text-mask` ecosystem, which offers similar masking solutions for various JavaScript frameworks and vanilla JS. The current stable version for `react-text-mask` is 5.5.0. Releases are typically driven by fixes and minor enhancements across its different framework-specific packages (e.g., React, Vue, Angular), rather than a fixed cadence. A key differentiator is its highly configurable `mask` prop, which accepts an array of characters and regular expressions, allowing for flexible pattern definition. It also supports a `render` prop for advanced customization, enabling integration with styled-components or other custom input wrappers. It aims for full compatibility with standard HTML `<input>` attributes like `className`, `placeholder`, `onBlur`, and `onChange`, simplifying its adoption into existing forms.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic usage of `MaskedInput` for phone number formatting, including placeholder, guide, and event handlers.

import React from 'react';
import MaskedInput from 'react-text-mask';

const PhoneNumberInput = () => (
  <div>
    <label htmlFor="phone-input">Phone Number:</label>
    <MaskedInput
      mask={['(', /[1-9]/, /\d/, /\d/, ')', ' ', /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/]}
      className="form-control"
      placeholderChar="_"
      placeholder="(   )    -    "
      guide={true}
      id="phone-input"
      onBlur={() => console.log('Input blurred')}
      onChange={(e) => console.log('Input changed:', e.target.value)}
    />
  </div>
);

export default PhoneNumberInput;

view raw JSON →