The Wayback Machine - https://web.archive.org/web/20240814183931/https://www.geeksforgeeks.org/create-otp-input-field-using-html-css-and-javascript/
Open In App

Create OTP Input Field using HTML CSS and JavaScript

Last Updated : 20 Dec, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In this article, we will explore how to build an OTP (One-Time Password) input box, which is commonly used on many websites. This interactive and engaging UI element allows users to enter their OTP conveniently and efficiently. We will walk through the step-by-step process of creating this functionality using HTML, CSS, and JavaScript.

Setting up the Project

To get started, set up the project structure by creating a folder called “OTP Input” and opening it in our preferred code editor. Within this folder, create three files: index.html, style.css, and script.js. HTML5 code is used in the index.html file and set up a live server for easier development and testing.

Approach

  • The OTP input box consists of a container, a group of input boxes, and an underline-style design.
  • Creating a container and assigning it a class of “container.” Inside the container, add a green color box that will hold the individual input boxes.
  • Each input box will have a class of “input,” a type of “text,” and an input mode of “numeric” to ensure a numeric keyboard is displayed on mobile devices.
  • Create four input boxes, and their length will be restricted to a single character.
  • To enable the input box navigation and interaction, we added JavaScript code. Target the orange color box and add an event listener to handle input events.
  • In the event callback, retrieve the entered value and check if it is a number. If it’s not, remove the value and return it. Next, identify the next input box using the “nextElementSibling” property and move the focus to it.
  • Additionally, add an event listener for key-up events to handle the delete operation.
  • When the backspace or delete key is pressed, move the focus to the previous input box and delete the value from the current input box.

Example: Below is the code example.

Javascript




// script.js
const inputs = document.getElementById("inputs");
 
inputs.addEventListener("input", function (e) {
    const target = e.target;
    const val = target.value;
 
    if (isNaN(val)) {
        target.value = "";
        return;
    }
 
    if (val != "") {
        const next = target.nextElementSibling;
        if (next) {
            next.focus();
        }
    }
});
 
inputs.addEventListener("keyup", function (e) {
    const target = e.target;
    const key = e.key.toLowerCase();
 
    if (key == "backspace" || key == "delete") {
        target.value = "";
        const prev = target.previousElementSibling;
        if (prev) {
            prev.focus();
        }
        return;
    }
});


HTML




<!-- index.html  -->
<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width,
                     initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>OTP Input</title>
    <link rel="stylesheet" href="style.css" />
</head>
 
<body>
    <div class="container">
        <div id="inputs" class="inputs">
            <input class="input" type="text"
                inputmode="numeric" maxlength="1" />
            <input class="input" type="text"
                inputmode="numeric" maxlength="1" />
            <input class="input" type="text"
                inputmode="numeric" maxlength="1" />
            <input class="input" type="text"
                inputmode="numeric" maxlength="1" />
        </div>
    </div>
    <script src="script.js"></script>
</body>
 
</html>


CSS




/* style.css */
.container {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
}
 
.input {
    width: 40px;
    border: none;
    border-bottom: 3px solid rgba(0, 0, 0, 0.5);
    margin: 0 10px;
    text-align: center;
    font-size: 36px;
    cursor: not-allowed;
    pointer-events: none;
}
 
.input:focus {
    border-bottom: 3px solid orange;
    outline: none;
}
 
.input:nth-child(1) {
    cursor: pointer;
    pointer-events: all;
}


Output: Click here to check the live Output.



Similar Reads

Create an OTP Verification Form in HTML CSS & JavaScript
When the application is loaded, there is a button through which the user can generate the OTP. Once the user generates the OTP, the form for verification becomes visible, the user needs to enter the correct OTP that is generated. When the user enters the OTP in the input field, and if the OTP matches then the OTP gets verified and the user can gene
4 min read
How to create OTP Input Box Using React ?
Let us explore the implementation of OTP (One-Time Password) login functionality using ReactJS. OTP-based logins add an extra layer of security to your applications, making them more resilient against unauthorized access. Prerequisites:ReactHTML, CSS and JavaScriptApproach:Create a React functional component managing the OTP input state using useSt
3 min read
OTP Generator and Validator Card using JavaScript & Tailwind CSS
OTP (One-Time Password) generator and validator built using Tailwind CSS and JavaScript allows users to generate a random OTP and validate it against the entered OTP. The application has a user-friendly interface for generating and validating OTPs using JavaScript for interactivity and Tailwind CSS for styling. Approach to create OTP Generator and
3 min read
Which input type is used to hide the input field to be displayed in HTML Form ?
The form tag is generally used to create forms for entering data. We can use different form tag attributes like text boxes, text area, checkboxes, radio buttons, buttons, dropdown list, etc. to collect data from the users. We need to use the HTML &lt;input type=”hidden”&gt; type for hiding the input field. Using this attribute, the input field will
1 min read
Create an OTP Generator and Validator App using React-Native
One-time passwords (OTPs) have become a popular choice for enhancing the security of various online services and applications. In this article, we'll explore how to create an OTP Generator and Validator App using React Native, a popular framework for building cross-platform mobile applications. Preview of final output: Let us have a look at how the
4 min read
How to create Underline Input Text Field using CSS ?
Input Text Field can be underlined by implementing different approaches to applying a bottom border to an input text field, using various methods such as HTML and CSS. Styling input text fields is a crucial aspect of web development, and adding a bottom border is a common way to enhance the visual appeal of these elements. Table of Content Using CS
2 min read
How to create a hidden input field in form using HTML ?
In this article, we will learn how to add a hidden input field into our form using HTML. A hidden control stores the data that is not visible to the user. It is used to send some information to the server which is not edited by the user. Normally, this hidden field usually contains the value which is common for all the users. The value must be reco
2 min read
How to validate if input date (end date) in input field must be after a given date (start date) using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In certain cases, we want the user to type a date that must come after some given date(Ex. 'end date' must be after 'start date')
5 min read
How to validate if input in input field exactly equals to some other value using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In certain cases, we want the user to type some exact value and based on that we give the user access to the request or deny the r
4 min read
How to validate if input in input field has alphanumeric characters only using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only alphanumeric characters are allowed i.e. there not allowed any special characters. We can also vali
4 min read
How to validate if input in input field has base64 encoded string using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. If In a certain input field only base 64 encoded string are allowed i.e there not allowed any other form of string which not const
4 min read
How to validate if input in input field has boolean value using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only boolean value i.e. true or false are allowed. We can also validate these input fields to accept onl
4 min read
How to validate if input in input field has base 32 encoded string using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. If In a certain input field, only base 32 encoded strings are allowed i.e. there not allowed any other form of string which does n
4 min read
How to validate if input in input field has alphabets only using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only alphabets are allowed i.e. there not allowed any number or special character. We can also validate
4 min read
How to validate if input in input field has BIC or Swift code only using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. When working with a bank system, In a certain input field only a valid Bank identification code(BIC) is allowed. BIC is an 8 or 11
5 min read
How to validate if input in input field has integer number only using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only integer numbers are allowed i.e. there not allowed any strings, special characters, or anything oth
4 min read
How to validate if input date (start date) in input field must be before a given date (end date) using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In certain cases, we want the user to type a date that must come before some given date (Ex: 'start date' must come before 'end da
5 min read
How to validate if input in input field has decimal number only using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only decimal numbers are allowed i.e. there not allowed any strings, special characters, or anything oth
4 min read
How to validate if input in input field is a valid date using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only a valid date is allowed i.e. there is not allowed any strings, number, or invalid date characters.
4 min read
How to validate if input in input field has lowercase letters only using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only lowercase letters are allowed. We can also validate these input fields to accept only lowercase let
4 min read
How to validate if input in input field is divisible by or multiple of a number using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only numbers that are multiples of a particular number are allowed i.e. there not allowed any letters, s
5 min read
How to validate if input in input field is a valid credit card number using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only valid credit card numbers are allowed i.e. there not allowed any other string or number which not f
5 min read
How to validate if input in input field has uppercase letters only using express-validator?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only uppercase letters are allowed. We can also validate these input fields to accept only uppercase let
4 min read
How to validate if input in input field has float number only using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only float numbers are allowed i.e. there not allowed any strings, special characters or anything other
4 min read
How to validate if input in input field must contains a seed word using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, we often need to validate input so that it must contain a seed word i.e. input can be anything(any strin
5 min read
How to validate if input in input field has full width string only using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only full-width strings are allowed. We can also validate these input fields to only accept full-width s
4 min read
How to validate if input in input field has hexadecimal number only using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only hexadecimal numbers are allowed. We can also validate these input fields to only accept full-width
4 min read
How to validate if input in input field has valid hexadecimal color only using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only valid hexadecimal color codes strings are allowed. We can also validate these input fields to only
4 min read
How to validate if input in input field has ASCII characters using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only Ascii characters are allowed i.e. there is not allowed any Non-ASCII characters (Ex: Ãą). We can als
4 min read
How to create input field that accept CSV file in HTML ?
CSV (Comma Separated Values) is a commonly used file format for storing tabular data. To allow users to upload CSV files to a web application, we can create an input field that accepts CSV files in HTML. There are different approaches to achieve this, ranging from the simple "accept" attribute in the input tag to more advanced third-party libraries
7 min read
three90RightbarBannerImg