You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

react-otp-input

Package Overview
Dependencies
Maintainers
3
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-otp-input

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

3.1.1
latest
Source
npmnpm
Version published
Maintainers
3
Created

What is react-otp-input?

The react-otp-input package is a React component for handling one-time password (OTP) inputs. It provides a customizable and easy-to-use interface for users to input OTPs, which are commonly used for two-factor authentication (2FA) and other security measures.

What are react-otp-input's main functionalities?

Basic OTP Input

This feature allows you to create a basic OTP input with a specified number of input fields. The `numInputs` prop determines the number of OTP input fields, and the `separator` prop allows you to customize the separator between the input fields.


import React, { useState } from 'react';
import OTPInput from 'react-otp-input';

const OTPComponent = () => {
  const [otp, setOtp] = useState('');

  return (
    <OTPInput
      value={otp}
      onChange={setOtp}
      numInputs={6}
      separator={<span>-</span>}
    />
  );
};

export default OTPComponent;

Custom Styling

This feature allows you to apply custom styles to the OTP input fields. The `inputStyle` prop accepts a style object that can be used to customize the appearance of each input field.


import React, { useState } from 'react';
import OTPInput from 'react-otp-input';

const OTPComponent = () => {
  const [otp, setOtp] = useState('');

  return (
    <OTPInput
      value={otp}
      onChange={setOtp}
      numInputs={6}
      inputStyle={{
        width: '2rem',
        height: '2rem',
        margin: '0 0.5rem',
        fontSize: '1.5rem',
        borderRadius: '4px',
        border: '1px solid rgba(0,0,0,0.3)'
      }}
    />
  );
};

export default OTPComponent;

Handling OTP Completion

This feature demonstrates how to handle the completion of OTP input. The `handleChange` function checks if the OTP length matches the expected number of inputs and performs an action (e.g., logging the OTP) when the OTP is complete.


import React, { useState } from 'react';
import OTPInput from 'react-otp-input';

const OTPComponent = () => {
  const [otp, setOtp] = useState('');

  const handleChange = (otp) => {
    setOtp(otp);
    if (otp.length === 6) {
      console.log('OTP Complete:', otp);
    }
  };

  return (
    <OTPInput
      value={otp}
      onChange={handleChange}
      numInputs={6}
    />
  );
};

export default OTPComponent;

Other packages similar to react-otp-input

Keywords

otp

FAQs

Package last updated on 12 Dec 2023

Did you know?

Socket

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.

Install

Related posts