react-verification-input
is a customizable, masked input that can be used to enter all sorts of codes e.g. security codes when two-factor authenticating. Also I'm sure you can think of many more creative use cases.
π΄ Try it out yourself!
β‘ Get Started Easily: The component works out of the box without the need to pass it a single prop.
π Highly Configurable: Change the behaviour of the component to fit your needs by passing props.
π πΌ Custom Styling: Customize the component's styling to match the look of your application.
π¨πΌβπ» TypeScript Support: The package contains TypeScript declarations so you can enjoy IntelliSense in your editor.
β Compatibility: Compatible with React 16 (>=16.8.0), React 17 and React 18. Support of new versions of React as they are released will be ensured.
First, install the package using npm or yarn.
Now, import the React component like this:
import VerificationInput from "react-verification-input";
And in your JSX write:
<VerificationInput />
That's it! You now have a basic verification input with default configuration rendered on your page. ππ
The component accepts a variety of props, which allow to configure the component according to your needs. For a complete API overview see here.
All of these props are optional and some also come with a default value. However, it's recommended to use at least the length
, validChars
and onChange
/onComplete
props.
Option | Type | Default | Description |
---|---|---|---|
value | String | - | The value of the verification input. Behaves like the value prop of a regular input element. This is necessary if the value needs to be changed from the outside (e.g. clearing the value). If you pass this prop, you are responsible to manage the value state. Otherwise the state will be kept inside the component. |
length | Number | 6 |
Number of characters the input should allow. |
validChars | String | 'A-Za-z0-9' |
Set of characters the input should allow. The string is inserted into a regexp character set ( /[]/ ) for input validation. |
placeholder | String | 'Β·' (U+00B7) |
The character to display in empty fields. In order to use the blank character as a placeholder, specify this option as ' ' or '' . |
autoFocus | Boolean | false |
Focus the input automatically as soon as it is rendered. |
passwordMode | Boolean | false |
Hide the input value by displaying * instead. |
inputProps | Object | {} |
The properties of this object get forwarded as props to the input element. |
containerProps | Object | {} |
The properties of this object get forwarded as props to the container element. |
classNames | Object | {} |
CSS class names to add to the specified elements. |
onChange | Function | - | Callback function that gets called with the string value whenever it changes. |
onComplete | Function | - | Callback function that gets called with the string value when the input is fully filled. |
onFocus | Function | - | Callback function that gets called when the component obtains focus. |
onBlur | Function | - | Callback function that gets called when the component loses focus. |
Style the input by passing it your custom class names like so:
<VerificationInput
classNames={{
container: "container",
character: "character",
characterInactive: "character--inactive",
characterSelected: "character--selected",
characterFilled: "character--filled",
}}
/>
π΄ View live example!
The following illustration shows the component structure.
![elements](https://private-user-images.githubusercontent.com/17298270/293507046-e0af9092-56e8-4791-a224-e964f9ddd775.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTA0MzUxNTQsIm5iZiI6MTcxMDQzNDg1NCwicGF0aCI6Ii8xNzI5ODI3MC8yOTM1MDcwNDYtZTBhZjkwOTItNTZlOC00NzkxLWEyMjQtZTk2NGY5ZGRkNzc1LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDAzMTQlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwMzE0VDE2NDczNFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWZjMDM3ZWVmYjJhYmM3YTFkNzc3NmNiODJiNzhlMjFmNjliZmVkMGI0ODgxMzMxNzk0MDJkZTgxYTNkMGE5MzEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.nHrch9bGRXLxsQvP7Y6_GhdVESg_dLQJv4oBjzCQol4)
Contributions welcome β€οΈ. If you want to contribute, feel free to have a look at the contribution guide.