otp-field

OTP Field Package

A customizable and easy-to-use OTP (One-Time Password) input field component for web applications. This package allows seamless integration of an OTP input field, with support for various features such as validation, theming, and accessibility.

Table of Contents

Features

  1. Configurable box count: Define the number of input boxes for the OTP field.
  2. Input validation: Restricts input to single characters and validates them based on the configured value - type (numeric, alphabetic, alphabetic lower, alphabetic upper, alphanumeric, alphanumeric lower, alphanumeric upper) or a custom regular expression.
  3. User interaction handling: Manages user interactions like input, paste, and keyboard events for smooth navigation and value management within the input boxes.
  4. Focus management: Automatically focuses on the appropriate input box based on user actions.
  5. Clear function: Clears all input boxes and resets the internal state.
  6. Destroy function: Removes the OTP field element from the DOM.

Installation

Usage

  1. Import the OTPField class:
import { OTPField } from 'otp-field';
  1. Define the configuration for your OTP field:
const otpFieldConfig = {
  namespace: 'otp', // Namespace for IDs (used for styling)
  boxCount: 6, // Number of input boxes
  // valueType: OTPValueType.NUMERIC, // Input type (numeric, alphabetic, alphanumeric)
  // customRegex: /your_custom_regex/, // Custom validation regex
  // onPasteBlur: true, // Blur the input box after pasting (default: true)
};
  1. Create an instance of the OTPField class with the configuration:
const otpField = new OTPField(otpFieldConfig);
  1. Build the OTP field and append it to the desired element in your DOM:
const containerElement = document.getElementById('otp-container');
otpField.build(containerElement);
  1. Access the current OTP value entered by the user:
const otpValue = otpField.value;
  1. SCSS import for styling:
@import '../../node_modules/@satyam-seth/otp-field/src/scss/field';

@include otp-field;

API Reference

Example

You can check out the example here

License

This package is licensed under the MIT License. See the LICENSE file for details.

Contributing

Feel free to open issues or submit pull requests for improvements or bug fixes!