Top Related Projects
Input mask for React, Angular, Ember, Vue, & plain JavaScript
React component to format numbers in an input or as a text.
Format input text content when you are typing...
Input Mask plugin
Quick Overview
React Input Mask is a lightweight and flexible input masking library for React applications. It allows developers to easily create masked input fields for various formats such as phone numbers, dates, and credit card numbers, enhancing user experience and data input accuracy.
Pros
- Easy integration with React components
- Supports custom mask patterns and placeholders
- Handles cursor position and selection intelligently
- Compatible with both controlled and uncontrolled input components
Cons
- Limited built-in mask patterns (requires custom implementation for complex masks)
- May have performance issues with large forms or many masked inputs
- Documentation could be more comprehensive
- Lacks some advanced features found in other masking libraries
Code Examples
- Basic phone number mask:
import InputMask from 'react-input-mask';
function PhoneInput() {
return <InputMask mask="+1 (999) 999-9999" />;
}
- Custom date mask with placeholder:
import InputMask from 'react-input-mask';
function DateInput() {
return (
<InputMask
mask="99/99/9999"
placeholder="DD/MM/YYYY"
maskChar={null}
/>
);
}
- Controlled input with mask:
import React, { useState } from 'react';
import InputMask from 'react-input-mask';
function CreditCardInput() {
const [value, setValue] = useState('');
return (
<InputMask
mask="9999 9999 9999 9999"
value={value}
onChange={(e) => setValue(e.target.value)}
/>
);
}
Getting Started
To use React Input Mask in your project, follow these steps:
-
Install the package:
npm install react-input-mask
-
Import and use the component in your React application:
import React from 'react'; import InputMask from 'react-input-mask'; function App() { return ( <div> <h1>Masked Input Example</h1> <InputMask mask="99/99/9999" placeholder="Enter a date (DD/MM/YYYY)" /> </div> ); } export default App;
-
Customize the mask pattern and other props as needed for your specific use case.
Competitor Comparisons
Input mask for React, Angular, Ember, Vue, & plain JavaScript
Pros of text-mask
- More flexible and customizable, supporting various input types beyond just text
- Provides addons for specific use cases like currency formatting
- Actively maintained with regular updates and bug fixes
Cons of text-mask
- Slightly more complex setup and configuration
- May have a steeper learning curve for beginners
- Larger bundle size due to additional features and flexibility
Code Comparison
text-mask:
import MaskedInput from 'react-text-mask'
<MaskedInput
mask={['(', /[1-9]/, /\d/, /\d/, ')', ' ', /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/]}
placeholder="Enter a phone number"
/>
react-input-mask:
import InputMask from 'react-input-mask'
<InputMask
mask="(999) 999-9999"
placeholder="Enter a phone number"
/>
Key Differences
- text-mask uses an array of RegExp and string literals for mask definition, while react-input-mask uses a string-based mask format
- text-mask offers more granular control over individual characters in the mask
- react-input-mask provides a simpler, more intuitive mask syntax for common use cases
Both libraries are popular choices for input masking in React applications, with text-mask offering more flexibility and features, while react-input-mask provides a simpler API for basic masking needs.
React component to format numbers in an input or as a text.
Pros of react-number-format
- More focused on number formatting, offering advanced features like prefix, suffix, and decimal scaling
- Supports custom formatting patterns with greater flexibility
- Provides better handling of decimal numbers and currency formatting
Cons of react-number-format
- Limited to number input formatting, unlike react-input-mask which supports various input types
- May have a steeper learning curve for simple use cases
- Lacks some features like dynamic masking based on input length
Code Comparison
react-number-format:
<NumberFormat
value={2456981}
displayType={'text'}
thousandSeparator={true}
prefix={'$'}
/>
react-input-mask:
<InputMask
mask="+1 (999) 999-9999"
value={phoneNumber}
onChange={handleChange}
/>
react-number-format is more suitable for complex number formatting scenarios, especially when dealing with currency or decimal values. It offers greater control over number representation but is limited to numerical inputs.
react-input-mask, on the other hand, provides a more general-purpose masking solution for various input types, including phone numbers, dates, and custom patterns. It's simpler to use for basic masking needs but may lack some advanced number formatting features.
Choose react-number-format for detailed number and currency formatting, and react-input-mask for general input masking across different data types.
Format input text content when you are typing...
Pros of Cleave.js
- Framework-agnostic, works with vanilla JavaScript and various frameworks
- Supports more input types (credit cards, numerals, date, time, phone)
- Smaller bundle size and potentially better performance
Cons of Cleave.js
- Less React-specific features and integration
- May require more setup for complex React applications
- Limited customization options for some mask types
Code Comparison
React Input Mask:
import InputMask from 'react-input-mask';
<InputMask
mask="+1 (999) 999-9999"
value={phone}
onChange={handleChange}
/>
Cleave.js:
import Cleave from 'cleave.js/react';
<Cleave
options={{
phone: true,
phoneRegionCode: 'US'
}}
value={phone}
onChange={handleChange}
/>
Both libraries offer similar functionality for basic input masking, but their implementation and usage differ slightly. React Input Mask provides a more straightforward mask definition, while Cleave.js uses an options object for configuration. Cleave.js offers more built-in formats, but React Input Mask allows for more flexible custom masks.
The choice between these libraries depends on specific project requirements, such as framework compatibility, input types needed, and desired level of customization.
Input Mask plugin
Pros of Inputmask
- More versatile, supporting various input types beyond just React
- Extensive options for customization and advanced masking features
- Larger community and more frequent updates
Cons of Inputmask
- Steeper learning curve due to more complex API
- Larger bundle size, which may impact performance in some applications
Code Comparison
react-input-mask:
import InputMask from 'react-input-mask';
<InputMask
mask="+7 (999) 999-99-99"
value={phoneNumber}
onChange={handleChange}
/>
Inputmask:
import Inputmask from 'inputmask';
Inputmask("+7 (999) 999-99-99").mask(document.getElementById("phone"));
Summary
Inputmask offers more flexibility and features but comes with increased complexity. react-input-mask is simpler and React-specific but may lack some advanced functionalities. The choice depends on project requirements, with Inputmask being better for complex masking needs across different frameworks, while react-input-mask is ideal for straightforward React applications.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
react-input-mask
Input masking component for React. Made with attention to UX.
This is a development branch for version 3.0. For the latest stable version see v2 branch.
Demo
Table of Contents
Installation
npm install react-input-mask@next --save
react-input-mask requires React 16.8.0 or later. If you need support for older versions, use version 2.
Usage
import React from "react"
import InputMask from "react-input-mask";
function DateInput(props) {
return <InputMask mask="99/99/9999" onChange={props.onChange} value={props.value} />;
}
Properties
Name | Type | Default | Description |
---|---|---|---|
mask | {String|Array<String, RegExp>} | Mask format | |
maskPlaceholder | {String} | _ | Placeholder to cover unfilled parts of the mask |
alwaysShowMask | {Boolean} | false | Whether mask prefix and placeholder should be displayed when input is empty and has no focus |
beforeMaskedStateChange | {Function} | Function to modify value and selection before applying mask | |
children | {ReactElement} | Custom render function for integration with other input components |
mask
Mask format. Can be either a string or array of characters and regular expressions.
<InputMask mask="99/99/99" />
Simple masks can be defined as strings. The following characters will define mask format:
Character | Allowed input |
---|---|
9 | 0-9 |
a | a-z, A-Z |
* | 0-9, a-z, A-Z |
Any format character can be escaped with a backslash.
More complex masks can be defined as an array of regular expressions and constant characters.
// Canadian postal code mask
const firstLetter = /(?!.*[DFIOQU])[A-VXY]/i;
const letter = /(?!.*[DFIOQU])[A-Z]/i;
const digit = /[0-9]/;
const mask = [firstLetter, digit, letter, " ", digit, letter, digit];
return <InputMask mask={mask} />;
maskPlaceholder
// Will be rendered as 12/--/--
<InputMask mask="99/99/99" maskPlaceholder="-" value="12" />
// Will be rendered as 12/mm/yy
<InputMask mask="99/99/99" maskPlaceholder="dd/mm/yy" value="12" />
// Will be rendered as 12/
<InputMask mask="99/99/99" maskPlaceholder={null} value="12" />
Character or string to cover unfilled parts of the mask. Default character is "_". If set to null
or empty string, unfilled parts will be empty as in a regular input.
alwaysShowMask
If enabled, mask prefix and placeholder will be displayed even when input is empty and has no focus.
beforeMaskedStateChange
In case you need to customize masking behavior, you can provide beforeMaskedStateChange
function to change masked value and cursor position before it's applied to the input.
It receieves an object with previousState
, currentState
and nextState
properties. Each state is an object with value
and selection
properites where value
is a string and selection is an object containing start
and end
positions of the selection.
- previousState: Input state before change. Only defined on
change
event. - currentState: Current raw input state. Not defined during component render.
- nextState: Input state with applied mask. Contains
value
andselection
fields.
Selection positions will be null
if input isn't focused and during rendering.
beforeMaskedStateChange
must return a new state with value
and selection
.
// Trim trailing slashes
function beforeMaskedStateChange({ nextState }) {
let { value } = nextState;
if (value.endsWith("/")) {
value = value.slice(0, -1);
}
return {
...nextState,
value
};
}
return <InputMask mask="99/99/99" maskPlaceholder={null} beforeMaskedStateChange={beforeMaskedStateChange} />;
Please note that beforeMaskedStateChange
executes more often than onChange
and must be pure.
children
To use another component instead of regular <input />
provide it as children. The following properties, if used, should always be defined on the InputMask
component itself: onChange
, onMouseDown
, onFocus
, onBlur
, value
, disabled
, readOnly
.
import React from 'react';
import InputMask from 'react-input-mask';
import MaterialInput from '@material-ui/core/Input';
// Will work fine
function Input(props) {
return (
<InputMask mask="99/99/9999" value={props.value} onChange={props.onChange}>
<MaterialInput type="tel" disableUnderline />
</InputMask>
);
}
// Will throw an error because InputMask's and children's onChange props aren't the same
function InvalidInput(props) {
return (
<InputMask mask="99/99/9999" value={props.value}>
<MaterialInput type="tel" disableUnderline onChange={props.onChange} />
</InputMask>
);
}
Known Issues
Autofill
Browser's autofill requires either empty value in input or value which exactly matches beginning of the autofilled value. I.e. autofilled value "+1 (555) 123-4567" will work with "+1" or "+1 (5", but won't work with "+1 (___) ___-____" or "1 (555)". There are several possible solutions:
- Set
maskChar
to null and trim space after "+1" withbeforeMaskedStateChange
if no more digits are entered. - Apply mask only if value is not empty. In general, this is the most reliable solution because we can't be sure about formatting in autofilled value.
- Use less formatting in the mask.
Please note that it might lead to worse user experience (should I enter +1 if input is empty?). You should choose what's more important to your users â smooth typing experience or autofill. Phone and ZIP code inputs are very likely to be autofilled and it's a good idea to care about it, while security confirmation code in two-factor authorization shouldn't care about autofill at all.
Cypress tests
The following sequence could fail
cy.get("input")
.focus()
.type("12345")
.should("have.value", "12/34/5___"); // expected <input> to have value 12/34/5___, but the value was 23/45/____
Since focus is not an action command, it behaves differently than the real user interaction and, therefore, less reliable.
There is a few possible workarounds
// Start typing without calling focus() explicitly.
// type() is an action command and focuses input anyway
cy.get("input")
.type("12345")
.should("have.value", "12/34/5___");
// Use click() instead of focus()
cy.get("input")
.click()
.type("12345")
.should("have.value", "12/34/5___");
// Or wait a little after focus()
cy.get("input")
.focus()
.wait(50)
.type("12345")
.should("have.value", "12/34/5___");
Thanks
Thanks to BrowserStack for the help with testing on real devices
Top Related Projects
Input mask for React, Angular, Ember, Vue, & plain JavaScript
React component to format numbers in an input or as a text.
Format input text content when you are typing...
Input Mask plugin
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot