clock  Mon - Sun 8.00 AM - 8.00 PM
fb
instagram
play store
pinterest

How to Create OTP Input Field using HTML, CSS and javaScript

writter  Sumit Dey Sarkar
Date  29 Mar 2023
Language  JavaScript
How to Create OTP Input Field using HTML, CSS and javaScript

How to Create OTP Input Field using HTML, CSS and javaScript

In this tutorial we will learn how to create OTP input field using HTML, CSS and javaScript.

Here you will know how to create OTP input field With Autofocus Input Fields using javaScript.

how to create OTP input field using HTML, CSS and javaScript

 

Overview

Step 1 - Create structure of OTP Input form.

Step 2 - Design your OTP input form using CSS.

Step 3 - Add functionality to OTP inputs using javaScript.

 

How to create OTP input field using HTML, CSS and javaScript

Follow below steps to create a otp input field using javascript.

 

Step 1 - Create structure of OTP input form

In first step you need to create structure of the OTP input form with HTML.

<html lang="en">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<head>
  <title>How to Create OTP Input Field Using HTML , CSS & Javascript</title>
</head>

<body>
  <section>
    <div class="main-form-div">
      <img src="https://www.teknowize.com/front-assets/images/logo/logo.png" alt="logo">
      <p class="form-heading-para">OTP FORM</p>
      <form action="javascript:void(0)">
        <div class="inner-form-div">
          <div class="all-inputs-div">
            <input type="text" class="form-control" placeholder="1" maxlength=1 oninput='digitValidate(this)'
              onkeyup='tabChange(1)'>
            <input type="text" class="form-control" placeholder="2" maxlength=1 oninput='digitValidate(this)'
              onkeyup='tabChange(2)'>
            <input type="text" class="form-control" placeholder="3" maxlength=1 oninput='digitValidate(this)'
              onkeyup='tabChange(3)'>
            <input type="text" class="form-control" placeholder="4" maxlength=1 oninput='digitValidate(this)'
              onkeyup='tabChange(4)'>

          </div>
          <button type="submit" class="submitButton">Verify OTP</button>
        </div>
      </form>
    </div>
  </section>

</body>

</html>
                    

 

Step 2 - Design your OTP input form using CSS

In this step we need design otp input form using CSS

                        * {
      box-sizing: border-box;
      margin: 0;
      padding: 0;
    }

    body {
      display: flex;
      justify-content: center;
      align-items: center;
    }

    section {
      width: 100%;
      display: flex;
      justify-content: center;
    }

    .form-heading-para {
      font-size: 24px;
      font-weight: 700;
      color: #333;
    }

    .main-form-div {
      background: #fdfffd;
      border: 1px solid #c300ff;
      box-shadow: 2px 2px 15px rgba(0, 0, 0, 0.03);
      border-radius: 8px;
      padding: 60px;
      text-align: center;
      margin-top: 30px;
      width: 640px;
    }

    .inner-form-div {
      position: relative;
      width: 100%;
    }

    .all-inputs-div {
      max-width: 370px;
      margin: 0 auto;
      margin-top: 1rem;
      display: flex;
      justify-content: space-between;
    }

    .all-inputs-div input {
      width: 100%;
      border: 1px solid #0044ff;
      border-radius: 50px;
      height: 50px;
      padding: 0 0.5rem;
      font-size: 18px;
      color: #333333;
      text-align: center;
      margin-left: 0.5rem;
    }

    .all-inputs-div input::placeholder {
      color: rgb(241, 240, 240);
    }

    .all-inputs-div input:first-child {
      margin-left: 0;
    }

    .all-inputs-div input:focus {
      outline: none;
      box-shadow: none;
    }

    .inner-form-div .submitButton {
      width: 250px;
      height: 50px;
      background: #00d17a;
      border-radius: 8px;
      font-weight: 600;
      font-size: 20px;
      text-align: center;
      color: #FFFFFF;
      margin-top: 25px;
      border: 1.5px solid #00d17a;
      cursor: pointer;
    }

    .inner-form-div .submitButton:hover {
      background: #ffffff;
      color: #00d17a;
    }

    @media (max-width:672px) {
      .main-form-div {
        width: 100%;
        padding: 25px;
      }
    }
          

 

Step 3 - Add functionality to OTP inputs using javaScript

In last step you need to add functionality to above OTP input form using javascript.

                    // OTP field full validation
    let digitValidate = function (ele) {
      ele.value = ele.value.replace(/[^0-9]/g, '');
    }
    let tabChange = function (val) {
      let ele = document.querySelectorAll('input');
      if (ele[val - 1].value != '') {
        ele[val].focus()
      }
      else if (ele[val - 1].value == '') {
        ele[val - 2].focus()
      }
    }

Now , your OTP form succefully created.

 

 

Lets, combine all the HTML, CSS and javaScript code together in a single file :

<html lang="en">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<head>
  <title>How to Create OTP Input Field Using HTML , CSS & Javascript</title>

<style>
       * {
      box-sizing: border-box;
      margin: 0;
      padding: 0;
    }

    body {
      display: flex;
      justify-content: center;
      align-items: center;
    }

    section {
      width: 100%;
      display: flex;
      justify-content: center;
    }

    .form-heading-para {
      font-size: 24px;
      font-weight: 700;
      color: #333;
    }

    .main-form-div {
      background: #fdfffd;
      border: 1px solid #c300ff;
      box-shadow: 2px 2px 15px rgba(0, 0, 0, 0.03);
      border-radius: 8px;
      padding: 60px;
      text-align: center;
      margin-top: 30px;
      width: 640px;
    }

    .inner-form-div {
      position: relative;
      width: 100%;
    }

    .all-inputs-div {
      max-width: 370px;
      margin: 0 auto;
      margin-top: 1rem;
      display: flex;
      justify-content: space-between;
    }

    .all-inputs-div input {
      width: 100%;
      border: 1px solid #0044ff;
      border-radius: 50px;
      height: 50px;
      padding: 0 0.5rem;
      font-size: 18px;
      color: #333333;
      text-align: center;
      margin-left: 0.5rem;
    }

    .all-inputs-div input::placeholder {
      color: rgb(241, 240, 240);
    }

    .all-inputs-div input:first-child {
      margin-left: 0;
    }

    .all-inputs-div input:focus {
      outline: none;
      box-shadow: none;
    }

    .inner-form-div .submitButton {
      width: 250px;
      height: 50px;
      background: #00d17a;
      border-radius: 8px;
      font-weight: 600;
      font-size: 20px;
      text-align: center;
      color: #FFFFFF;
      margin-top: 25px;
      border: 1.5px solid #00d17a;
      cursor: pointer;
    }

    .inner-form-div .submitButton:hover {
      background: #ffffff;
      color: #00d17a;
    }

    @media (max-width:672px) {
      .main-form-div {
        width: 100%;
        padding: 25px;
      }
    }
</style>

</head>

<body>
  <section>
    <div class="main-form-div">
      <img src="https://www.teknowize.com/front-assets/images/logo/logo.png" alt="logo">
      <p class="form-heading-para">OTP FORM</p>
      <form action="javascript:void(0)">
        <div class="inner-form-div">
          <div class="all-inputs-div">
            <input type="text" class="form-control" placeholder="1" maxlength=1 oninput='digitValidate(this)'
              onkeyup='tabChange(1)'>
            <input type="text" class="form-control" placeholder="2" maxlength=1 oninput='digitValidate(this)'
              onkeyup='tabChange(2)'>
            <input type="text" class="form-control" placeholder="3" maxlength=1 oninput='digitValidate(this)'
              onkeyup='tabChange(3)'>
            <input type="text" class="form-control" placeholder="4" maxlength=1 oninput='digitValidate(this)'
              onkeyup='tabChange(4)'>

          </div>
          <button type="submit" class="submitButton">Verify OTP</button>
        </div>
      </form>
    </div>
  </section>

<script>
   // OTP field full validation
    let digitValidate = function (ele) {
      ele.value = ele.value.replace(/[^0-9]/g, '');
    }
    let tabChange = function (val) {
      let ele = document.querySelectorAll('input');
      if (ele[val - 1].value != '') {
        ele[val].focus()
      }
      else if (ele[val - 1].value == '') {
        ele[val - 2].focus()
      }
    }                    
</script>

</body>

</html>
                    

 

Output

Check output here https://www.teknowize.com/snipits/otp-verification-form/how-to-create-otp-input-field-using-html-css-js

Comments 0

Leave a comment

Coursera, Codeacademy, Udacity, W3Schools, Udemy, Alison, TheNewBoston, edX, P.S.Codewars,Freecodecamp, Managing technical debt blog, Scrimba, Codepen, Codepen/challenges, The Odin Project, htmlreference.​io, cssreference.​io, Frontend Mentor, Dev Challenges, MDN, Code Mentor, Coding Dojo, CSS Battle, Codier, Ace Frontend, Can I Use, CSS Tricks, 30 Seconds of Code,tutorialspoint, Neumorphism, Shaddows Brumm, Fancy Border Radius, Glow Generator, Clothoid Corners, Glassmorphism, Clipy, CSS Filters, Base64 Image, Quantity Queries, Animations, Cubic-Bezier, Keyframes, Wait Animate, Transition.Style, graphic design, web design, website design, website builder, web developer, web designer, webdesign, ecommerce website, web design company, website creator, website designer, responsive web design, web development company, best website design, web design software, web page design, build a website, web developer salary, design website, web design courses, how to design a website, web design inspiration, website layout, web designer salary, web application development, ecommerce website design, web agency, software development company, web design tutorial, web programming, design company, website design templates, what is web designing, web developer jobs, website developer, web design agency, freelance web developer, web design services, freelance web designer, graphic design websites, web solutions, ecommerce website development, free website design, web development courses, webdev, web developers, web development tools, website design services, developpeur web, web design london, website design ideas, web designing and programming, design a website, web design and development, web dev, web development services, homepage design, best designed websites, cheap website design, learn web design, web design templates, web design tools, web design jobs, website design inspiration, web design india, flash website, website developers, designer websites, website services, website design cost, good website design, site design, simple website design, cool website designs, modern website design, graphic designer websites, webcode, best web design software, website making, free web design software, mobile website design, learn web development, front end web developer, how to become a web developer, web developer portfolio, web development company in india, python web development, web development tutorial, website company, website design and development, web company, webdesigning, professional website design, affordable web design, best web design company, creative web design, top website designs, website design pricing, web developer tools, how to develop a website