Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
@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 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 inputProps = useInputMask({ mask: '+(999) 999-9999' });
return (
<input name="phone" {...inputProps} 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 a phone number input field. The mask prop specifies the format of the phone number.
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" |
import React from 'react';
import { useInputMask } from 'react-input-mask';
const MyComponent = () => {
const inputProps = useInputMask({ mask: '+(999) 999-9999' });
return (
<input name="phone" {...inputProps} />
);
};
import React from 'react';
import { useInputMask } from 'react-input-mask';
const MyComponent = () => {
const inputProps = useInputMask({ mask: '999-99-9999' });
return (
<input name="ssn" {...inputProps} />
);
};
import React from 'react';
import { useInputMask } from 'react-input-mask';
const MyComponent = () => {
const inputProps = useInputMask({ mask: '9999 9999 9999 9999' });
return (
<input name="cc" {...inputProps} />
);
};
import React from 'react';
import { useForm } from 'react-hook-form';
import { useInputMask } from 'react-input-mask';
const MyComponent = () => {
const { register, handleSubmit } = useForm();
const inputProps = useInputMask({ mask: '9999 9999 9999 9999' });
return (
<form onSubmit={handleSubmit(data => console.log(data))}>
<input {...register('cc')} {...inputProps} />
<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 28 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
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.