OTP Input box
Watch the walkthrough
Open on InstagramExercise
Build four one-character OTP fields with forward input focus, backward deletion, and paste.
What the source implements
The source moves focus after any character, not just digits. Paste starts at the first field and does not prevent the browser default paste. As extensions, sanitize digits, start from the focused position, prevent duplicate insertion, and expose the joined string.
Run and revise
Copy the entire fenced HTML into a file with the same name and open it in a browser. It includes the markup, styles, and JavaScript together. Check the console, keyboard behavior, and boundary cases. Remote APIs/assets and CDN scripts need network access.
Original repository source
The complete file below is preserved apart from line endings; the extensions above are not changes to this copied implementation.
6-otp-input-box.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,400;0,700;1,100&display=swap"
rel="stylesheet"
/>
<style>
body {
min-width: 350px;
max-width: 400px;
margin: 10rem auto;
color: white;
background-color: rgb(71, 79, 87);
font-family: "Poppins", sans-serif;
font-weight: 400;
font-style: normal;
}
input {
flex-grow: 1;
padding: 0.35rem 1rem;
border: 1px solid gainsboro;
border-radius: 5px;
}
button {
padding: 0.5rem 1rem;
min-width: 8rem;
cursor: pointer;
background-color: rgb(69, 119, 211);
color: white;
font-weight: 700;
border: none;
border-radius: 5px;
transition: all 0.2s ease;
}
button:hover {
background-color: rgb(55, 102, 191);
}
button:active {
scale: 0.9;
}
button:disabled {
cursor: not-allowed;
background-color: lightgray;
}
</style>
</head>
<style>
.input {
width: 1rem;
margin: 1rem;
padding: 1rem;
font-size: large;
font-weight: bold;
}
</style>
<body>
<h3>OTP Input Box</h3>
<div id="input-wrapper"></div>
</body>
<script>
const inputBoxs = 4;
const inputWrapper = document.getElementById("input-wrapper");
// Triggered after each keypress, If input has length 1 move to next box
inputWrapper.addEventListener("input", (e) => {
if (e.target.value.length === 1) {
console.log("input is of length 1, move to next box");
const currentIndex = e.target.getAttribute("data-index");
const nextInputBox = document.querySelector(
`[data-index="${+currentIndex + 1}"]`
);
nextInputBox?.focus();
}
});
// Triggered after each keypress, If Backspace was pressed
// And input has length 1 - do nothing let backspace clear it.
// input has length 0 - move to prev input box
inputWrapper.addEventListener("keydown", (e) => {
if (e.key === "Backspace") {
if (e.target.value) {
console.log("Input has some value, let it get cleared first");
return;
}
console.log("Backspace pressed, move to prev box");
const currentIndex = e.target.getAttribute("data-index");
const prevInputBox = document.querySelector(
`[data-index="${+currentIndex - 1}"]`
);
prevInputBox?.focus();
}
});
inputWrapper.addEventListener("paste", (e) => {
const data = e.clipboardData.getData("text").split("");
data.forEach((num, index) => {
if (index < inputBoxs) {
const currInputBox = document.querySelector(
`[data-index="${index}"]`
);
currInputBox.value = num;
currInputBox.focus();
}
});
});
function init() {
for (let i = 0; i < inputBoxs; i++) {
const input = document.createElement("input");
input.classList = "input";
input.maxLength = 1;
input.setAttribute("data-index", i);
inputWrapper.appendChild(input);
}
}
init();
</script>
</html>