<html lang="en">
<head>
<!-- Design by foolishdeveloper.com -->
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>OTP Input</title>
<!-- Google Font -->
<link
href="https://fonts.googleapis.com/css2?family=Poppins:wght@500&display=swap"
rel="stylesheet"
/>
<!-- Stylesheet -->
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="container">
<div class="inputfield">
<input type="number" maxlength="1" class="input" disabled />
<input type="number" maxlength="1" class="input" disabled />
<input type="number" maxlength="1" class="input" disabled />
<input type="number" maxlength="1" class="input" disabled />
</div>
<button class="hide" id="submit" onclick="validateOTP()">Submit</button>
</div>
<!-- Script -->
<script src="script.js"></script>
</body>
</html>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
height: 100vh;
background: rgb(28, 134, 255)
}
.container {
width: 28em;
background-color: #ffffff;
padding: 4em 2em;
position: absolute;
transform: translate(-50%, -50%);
top: 50%;
left: 50%;
border-radius: 0.8em;
box-shadow: 0 45px 60px rgba(30, 22, 1, 0.3);
}
.inputfield {
width: 100%;
display: flex;
justify-content: space-around;
}
.input {
height: 3em;
width: 3em;
border: 2px solid #dad9df;
outline: none;
text-align: center;
font-size: 1.5em;
border-radius: 0.3em;
background-color: #ffffff;
outline: none;
/*Hide number field arrows*/
appearance: textfield;
}
input[type="number"]::outer-spin-button,
input[type="number"]::inner-spin-button {
appearance: none;
margin: 0;
}
#submit {
background-color: #044ecf;
border: none;
outline: none;
font-size: 1.2em;
padding: 0.8em 2em;
color: #ffffff;
border-radius: 0.1em;
margin: 1em auto 0 auto;
cursor: pointer;
}
.show {
display: block;
}
.hide {
display: none;
}
.input:disabled {
color: #89888b;
}
.input:focus {
border: 3px solid #ffb800;
}
//Initial references
const input = document.querySelectorAll(".input");
const inputField = document.querySelector(".inputfield");
const submitButton = document.getElementById("submit");
let inputCount = 0,
finalInput = "";
//Update input
const updateInputConfig = (element, disabledStatus) => {
element.disabled = disabledStatus;
if (!disabledStatus) {
element.focus();
} else {
element.blur();
}
};
input.forEach((element) => {
element.addEventListener("keyup", (e) => {
e.target.value = e.target.value.replace(/[^0-9]/g, "");
let { value } = e.target;
if (value.length == 1) {
updateInputConfig(e.target, true);
if (inputCount <= 3 && e.key != "Backspace") {
finalInput += value;
if (inputCount < 3) {
updateInputConfig(e.target.nextElementSibling, false);
}
}
inputCount += 1;
} else if (value.length == 0 && e.key == "Backspace") {
finalInput = finalInput.substring(0, finalInput.length - 1);
if (inputCount == 0) {
updateInputConfig(e.target, false);
return false;
}
updateInputConfig(e.target, true);
e.target.previousElementSibling.value = "";
updateInputConfig(e.target.previousElementSibling, false);
inputCount -= 1;
} else if (value.length > 1) {
e.target.value = value.split("")[0];
}
submitButton.classList.add("hide");
});
});
window.addEventListener("keyup", (e) => {
if (inputCount > 3) {
submitButton.classList.remove("hide");
submitButton.classList.add("show");
if (e.key == "Backspace") {
finalInput = finalInput.substring(0, finalInput.length - 1);
updateInputConfig(inputField.lastElementChild, false);
inputField.lastElementChild.value = "";
inputCount -= 1;
submitButton.classList.add("hide");
}
}
});
const validateOTP = () => {
alert("Success");
};
//Start
const startInput = () => {
inputCount = 0;
finalInput = "";
input.forEach((element) => {
element.value = "";
});
updateInputConfig(inputField.firstElementChild, false);
};
window.onload = startInput();
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.