
Security News
Software Engineering Daily Podcast: Feross on AI, Open Source, and Supply Chain Risk
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.
@code-forge/react-input-mask
Advanced tools
React Input Mask is an open source library for React that provides an easy way to apply masks to input fields.
Masks can be used to enforce specific formatting for user input, such as phone numbers, social security numbers, and credit card numbers. With React Input Mask, you can define a mask for your input field, and the library will automatically format the user's input according to the specified mask.
React Input Mask is a lightweight library that provides a simple API for applying masks to input fields. It is built on top of React Hooks, and is designed to be used with functional components. It is also compatible with libraries such as react-hook-form. The design philosophy behind React Input Mask is to provide a simple API that is easy to use and understand via a hook instead of a component.
It is also designed to be used with Next.js and Remix.run and other server-side rendering frameworks.
React Input Mask can be installed via npm or yarn:
npm install @code-forge/react-input-mask
yarn add @code-forge/react-input-mask
Using React Input Mask is easy. Simply import the useInputMask hook from the library, and pass the mask prop to the hook. The hook will return an object containing the value and onMouseDown props that you can pass to your input field.
import React from 'react';
import { useInputMask } from 'react-input-mask';
const MyComponent = () => {
const { getInputProps } = useInputMask({ mask: 'One does not simply walk into AAAAAA' });
return (
<input name="phone" {...getInputProps()} onChange={e => {
// Your onChange handler gets the output of the hook (won't trigger if the input is invalid)
console.log(e.target.value);
}} />
);
};
In the example above, we've applied a mask to fill the word needed to complete the sentence.
You can customize the mask to fit your needs by using a variety of special characters that represent different types of input.
| Character | Description |
|---|---|
| 9 | Represents a number. |
| A | Represents a letter. |
| * | Represents a wildcard. |
| Prop | Type | Description | Default |
|---|---|---|---|
| mask | string | The mask to apply to the input field. | undefined |
| placeholderChar | string | The character to use as a placeholder for the mask characters (eg. 999-999 with placeholderChar set to "@" will produce @@@-@@@). | "_" |
| charRegex | RegExp | A regular expression that represents the characters that are allowed to be entered into the input field. | /^[a-zA-Z]*$/ |
| numRegex | RegExp | A regular expression that represents the numbers that are allowed to be entered into the input field. | /^[0-9]*$/ |
| type | "raw" or "mask" | The type of value to return from the hook. If set to "raw", the hook will return the raw value of the input field (eg. mask 999-999-99 with 111-111-11 will output 11111111). If set to "mask", the hook will return the masked value of the input field. (eg. mask 999-999-99 with 111-111-11 will output 111-111-11) | "raw" |
| value | string | The initial value of the input field. (The hook expects to be given a value created by itself, if you provide an invalid value it will try to fill as much of the mask as it can but relies on you to pass it a positive value, otherwise it will default to its default values) | undefined |
import React from 'react';
import { useInputMask } from 'react-input-mask';
const MyComponent = () => {
const { getInputProps } = useInputMask({ mask: '+(999) 999-9999' });
return (
<input name="phone" {...getInputProps()} />
);
};
import React from 'react';
import { useInputMask } from 'react-input-mask';
const MyComponent = () => {
const { getInputProps } = useInputMask({ mask: '999-99-9999' });
return (
<input name="ssn" {...getInputProps()} />
);
};
import React from 'react';
import { useInputMask } from 'react-input-mask';
const MyComponent = () => {
const { getInputProps } = useInputMask({ mask: '9999 9999 9999 9999' });
return (
<input name="cc" {...getInputProps()} />
);
};
import React from 'react';
import { useInputMask } from 'react-input-mask';
const MyComponent = () => {
const { getInputProps } = useInputMask({ mask: '9999 9999 9999 9999' });
const maskProps = getInputProps();
return (
<input name="cc" onKeyDown={e => {
// do something
maskProps.onKeyDown(e);
}} value={maskProps.value} />
);
};
import React from 'react';
import { useForm } from 'react-hook-form';
import { useInputMask } from 'react-input-mask';
const MyComponent = () => {
const { register, handleSubmit } = useForm();
const { getInputProps } = useInputMask({ mask: '9999 9999 9999 9999' });
return (
<form onSubmit={handleSubmit(data => console.log(data))}>
<input {...register('cc')} {...getInputProps()} />
<button type="submit">Submit</button>
</form>
);
};
If you like the project, please consider supporting us by giving a ⭐️ on Github.
MIT
If you find a bug, please file an issue on our issue tracker on GitHub
Thank you for considering contributing to react-input-mask! We welcome any contributions, big or small, including bug reports, feature requests, documentation improvements, or code changes.
To get started, please fork this repository and make your changes in a new branch. Once you're ready to submit your changes, please open a pull request with a clear description of your changes and any related issues or pull requests.
Please note that all contributions are subject to our Code of Conduct. By participating, you are expected to uphold this code.
We appreciate your time and effort in contributing to react-input-mask and helping to make it a better tool for the community!
FAQs
React input mask used to mask input fields on the fly.
The npm package @code-forge/react-input-mask receives a total of 179 weekly downloads. As such, @code-forge/react-input-mask popularity was classified as not popular.
We found that @code-forge/react-input-mask demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.

Security News
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.

Security News
GitHub has revoked npm classic tokens for publishing; maintainers must migrate, but OpenJS warns OIDC trusted publishing still has risky gaps for critical projects.

Security News
Rust’s crates.io team is advancing an RFC to add a Security tab that surfaces RustSec vulnerability and unsoundness advisories directly on crate pages.