
Company News
Socket Named Top Sales Organization by RepVue
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.
react-text-mask
Advanced tools
First, install it.
npm i react-text-mask --save
Then, require it and use it.
import React from 'react'
import MaskedInput from 'react-text-mask'
export default () => (
<div>
<MaskedInput
mask={['(', /[1-9]/, /\d/, /\d/, ')', ' ', /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/]}
/>
</div>
)
<MaskedInput/> is fully compatible with <input/> element. So, you can
pass it CSS classes, a placeholder attribute, or even an onBlur handler.
For example, the following works:
<MaskedInput
mask={['(', /[1-9]/, /\d/, /\d/, ')', ' ', /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/]}
className="form-control"
placeholder="Enter a phone number"
guide={false}
id="my-input-id"
onBlur={() => {}}
onChange={() => {}}
/>
For more information about the props that you can pass to the component, see
the documentation here.
To see an example of the code running, follow these steps:
git clone git@github.com:text-mask/text-mask.gitcd text-masknpm installnpm run react:devThe code of the example is in react/example.
<input> ComponentFor advanced uses, you can customize the rendering of the resultant <input> via a render prop.
This is entirely optional, if no render prop is passed, a normal <input> is rendered.
For example, to use with styled-components, which requires an innerRef:
<MaskedInput
mask={['(', /[1-9]/, /\d/, /\d/, ')', ' ', /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/]}
placeholder="Enter a phone number"
id="my-input-id"
render={(ref, props) => (
<MyStyledInput innerRef={ref} {...props} />
)}
/>
const MyStyledInput = styled.input`
background: papayawhip;
`;
#804 In some cases different values in event.target.value and in input element
We would love some contributions! Check out this document to get started.
react-input-mask is another popular library for creating input masks in React applications. It offers similar functionality to react-text-mask but is often praised for its simplicity and ease of use. It supports both controlled and uncontrolled components and provides a straightforward API for defining masks.
Cleave.js is a JavaScript library for formatting input fields. It can be used with React to create input masks for various formats such as credit card numbers, phone numbers, and dates. Cleave.js is known for its flexibility and extensive customization options, making it a strong alternative to react-text-mask.
react-maskedinput is a lightweight library for creating masked input fields in React. It provides a simple API for defining masks and supports a wide range of input formats. While it may not have as many features as react-text-mask, it is a good choice for developers looking for a minimalistic solution.
FAQs
React input component that accepts mask pattern
The npm package react-text-mask receives a total of 404,672 weekly downloads. As such, react-text-mask popularity was classified as popular.
We found that react-text-mask demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.

Company News
/Security News
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.