Skip to content
On this page

React OTP Input

A fully customizable, one-time password input component for the web built with React.

see here

Support auto fill on mobile (safari, chrome....)

see here

To install the latest stable version:

npm install --save reactjs-otp-input@latest

Or yarn

yarn add reactjs-otp-input

Basic usage:

tsx
import React, { Component } from 'react';
import { OtpInput } from 'react-otp-input';

export default const App {
  const [otp, setOtp] = useState('');

  const handleChange = (otp) => setOtp({ otp });

  return <OtpInput value={otp} onChange={handleChange} numInputs={6} separator={<span>-</span>} />;
}

API

Name
TypeRequiredDefaultDescription
numInputsnumbertrue4Number of OTP inputs to be rendered.
onChangefunctiontrueconsole.logReturns OTP code typed in inputs.
valuestring / numbertrue''The value of the OTP passed into the component.
placeholderstringfalsenoneSpecify an expected value of each input. The length of this string should be equal to numInputs.
separatorcomponent
falsenoneProvide a custom separator between inputs by passing a component. For instance, <span>-</span> would add - between each input.
containerStylestyle (object) / className (string)falsenoneStyle applied or class passed to container of inputs.
inputStylestyle (object) / className (string)falsenoneStyle applied or class passed to each input.
focusStylestyle (object) / className (string)falsenoneStyle applied or class passed to inputs on focus.
isDisabledbooleanfalsefalseDisables all the inputs.
disabledStylestyle (object) / className (string)falsenoneStyle applied or class passed to each input when disabled.
hasErroredbooleanfalsefalseIndicates there is an error in the inputs.
errorStylestyle (object) / className (string)falsenoneStyle applied or class passed to each input when errored.
shouldAutoFocusbooleanfalsefalseAuto focuses input on initial page load.
isInputNumbooleanfalsefalseRestrict input to only numbers.
isInputSecurebooleanfalsefalseMasks input characters.
data-cystringfalse-Test attribute passed to the inputs.
data-testidstringfalse-Test attribute passed to the inputs.

About

Buy Me A Coffee

Gia Hung – hung.hg