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.
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