Skip to content
No results
  • Home
  • Quizzes
    • HTML Quiz
    • CSS Quiz
    • JavaScript Quiz
  • HTML & CSS
    • Card Designs
    • Navbars
    • Login Forms
  • JavaScript
    • JavaScript Games
    • API Projects
  • Web Development
    • Frontend
    • Backend
  • Interview Questions
  • Home
  • About us
  • Contact Us
  • Privacy Policy
  • Quizzes
Coding Torque

Learn by projects๐Ÿš€

  • Home
  • Quizzes
    • HTML Quiz
    • CSS Quiz
    • JavaScript Quiz
  • HTML & CSS
    • Card Designs
    • Navbars
    • Login Forms
  • JavaScript
    • JavaScript Games
    • API Projects
  • Web Development
    • Frontend
    • Backend
  • Interview Questions
Coding Torque

Learn by projects๐Ÿš€

OTP input field form using html css and javascript

OTP input field using HTML & CSS

  • Piyush PatilPiyush Patil
  • March 6, 2023
  • HTML & CSS, Beginner, Frontend, JavaScript, Login Forms, Web Development
Share your love

Hello, coding enthusiasts! Are you interested in improving your front-end development skills? In this tutorial, we’ll be exploring HTML and CSS to create a sleek and modern One-Time Password (OTP) input field. OTP is a vital security feature used in many applications and websites today, and creating a user-friendly and intuitive OTP input field is crucial for enhancing the user experience. By following this tutorial, you will learn how to implement various HTML and CSS techniques, including form design, animations, and hover effects. So, let’s dive into the world of front-end development and create a stunning OTP input field together!

Before we start here are some more Projects you might like to try –

  • Deploying a MERN App with Multiple Domain Names on VPS
  • 3D CSS Logo with Pure CSS
  • 3D Santa using HTML and CSS Only

I would recommend you don’t just copy and paste the code, just look at the code and type by understanding it.

HTML Code 

Starter Template

<!doctype html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Font Awesome CDN  -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css"
        integrity="sha512-1PKOgIY59xJ8Co8+NE6FZ+LOAZKjy+KY8iq0G4B3CyeY6wYHN3yt9PW0XpSriVlkMXe40PTKnXrLnZ9+fkDaog=="
        crossorigin="anonymous" />

    <!-- CSS -->
    <link rel="stylesheet" href="style.css">

    <title>OTP Field Form - Coding Torque</title>
</head>

<body>
    <!-- Further code here -->

    <script src="script.js"></script>

</body>

</html>

Paste the below code in your <body> tag.

<p>OTP sent on ....12@gmail.com</p>
<h1>Enter OTP</h1>
<div class="otp-field">
    <input type="text" maxlength="1" />
    <input type="text" maxlength="1" />
    <input class="space" type="text" maxlength="1" />
    <input type="text" maxlength="1" />
    <input type="text" maxlength="1" />
    <input type="text" maxlength="1" />
</div>

Output Till Now

CSS Code 

Create a file style.css and paste the code below.

body {
  margin: 0;
  font-family: "Poppins", sans-serif;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  background: #282a36;
  height: 100vh;
  color: #fff;
}

.otp-field {
  display: flex;
}

.otp-field input {
  width: 24px;
  font-size: 32px;
  padding: 10px;
  text-align: center;
  border-radius: 5px;
  margin: 2px;
  border: 2px solid #55525c;
  background: #21232d;
  font-weight: bold;
  color: #fff;
  outline: none;
  transition: all 0.1s;
}

.otp-field input:focus {
  border: 2px solid #a527ff;
  box-shadow: 0 0 2px 2px #a527ff6a;
}

.disabled {
  opacity: 0.5;
}

.space {
  margin-right: 1rem !important;
}

Output Till Now

OTP input field form using html css and javascript

JavaScript Code 

Create a file script.js and paste the code below.

const inputs = document.querySelectorAll(".otp-field input");

inputs.forEach((input, index) => {
    input.dataset.index = index;
    input.addEventListener("keyup", handleOtp);
    input.addEventListener("paste", handleOnPasteOtp);
});

function handleOtp(e) {
    /**
     * <input type="text" ๐Ÿ‘‰ maxlength="1" />
     * ๐Ÿ‘‰ NOTE: On mobile devices `maxlength` property isn't supported,
     * So we to write our own logic to make it work. ๐Ÿ™‚
     */
    const input = e.target;
    let value = input.value;
    let isValidInput = value.match(/[0-9a-z]/gi);
    input.value = "";
    input.value = isValidInput ? value[0] : "";

    let fieldIndex = input.dataset.index;
    if (fieldIndex < inputs.length - 1 && isValidInput) {
        input.nextElementSibling.focus();
    }

    if (e.key === "Backspace" && fieldIndex > 0) {
        input.previousElementSibling.focus();
    }

    if (fieldIndex == inputs.length - 1 && isValidInput) {
        submit();
    }
}

function handleOnPasteOtp(e) {
    const data = e.clipboardData.getData("text");
    const value = data.split("");
    if (value.length === inputs.length) {
        inputs.forEach((input, index) => (input.value = value[index]));
        submit();
    }
}

function submit() {
    console.log("Submitting...");
    // ๐Ÿ‘‡ Entered OTP
    let otp = "";
    inputs.forEach((input) => {
        otp += input.value;
        input.disabled = true;
        input.classList.add("disabled");
    });
    console.log(otp);
    // ๐Ÿ‘‰ Call API below
}

Written by: Piyush Patil

If you have any doubts or any project ideas feel free to Contact Us

Hope you find this post helpful๐Ÿ’–

Share your love
text to speech converter using javascript
Previous Post Text to Speech Converter using JavaScript
Next Post Detect Key Pressed using JavaScript
detect key pressed using javascript
Recent Posts
  • Ying & Yang Cats with hover effect using HTML and CSS

    Ying & Yang Cats with hover effect using HTML and CSS

    12 months ago
  • Animated Button with Bird Effect using HTML and CSS

    Animated Button with Bird Effect using HTML and CSS

    12 months ago
  • Cubes with Hover Effects using HTML and CSS

    Cubes with Hover Effects using HTML and CSS

    12 months ago
  • X-Ray using HTML and CSS

    X-Ray using HTML and CSS

    12 months ago
  • Skillset icons with CSS Animation

    Skillset icons with CSS Animation

    12 months ago
Categories
Advance Algorithms API Projects App Development Backend Beginner Blockchain Development Bootstrap Card Designs Cheatsheets Coding CSS Games CSS Quiz Express JS Frontend Hero Section HTML & CSS HTML Quiz Intermediate Interview Questions JavaScript JavaScript Games JavaScript Quiz Login Forms MongoDB Navbars Next JS Node JS Products Programming Python Python Games Quizzes React React Quiz Reel Roadmaps SCSS TailwindCSS Toggle Buttons Uncategorized Web Design Web Development

Related Posts

Deploying a MERN App with Multiple Domain Names on VPS

  • Piyush Patil
  • February 13, 2025
  • HTML & CSS, API Projects, Backend, Express JS, Frontend, MongoDB, Next JS, React, Web Development

3D CSS Logo with Pure CSS

  • Piyush Patil
  • December 20, 2024
  • HTML & CSS

3D Santa using HTML and CSS Only

  • Piyush Patil
  • December 19, 2024
  • HTML & CSS
About Us

Coding Torque is a platform where we share amazing projects related Web development, App development, Software development, etc with Source Code.

Pages
  • About us
  • Contact Us
  • Privacy Policy
  • Quizzes
No results
Recent Posts
  • Crafting a Pure CSS 3D Breaking Bad Truck Scene

    Crafting a Pure CSS 3D Breaking Bad Truck Scene

    6 months ago
  • Creating a CSS 3D Chess Board with Pieces

    Creating a CSS 3D Chess Board with Pieces

    6 months ago

Copyright ยฉ 2025 - Coding Torque | Design by Piyush Patil