Create OTP Input Field using HTML CSS and JavaScript (original) (raw)

Last Updated : 31 Mar, 2026

This project demonstrates how to build an OTP (One-Time Password) input box using HTML, CSS, and JavaScript for a smooth and user-friendly verification experience. It focuses on creating an interactive UI that enhances usability and input efficiency.

**Approach

OTP Input

CSS

/* style.css */ .container { display: flex; justify-content: center; align-items: center; min-height: 100vh; }

.input { width: 40px; border: none; border-bottom: 3px solid rgba(0, 0, 0, 0.5); margin: 0 10px; text-align: center; font-size: 36px; cursor: not-allowed; pointer-events: none; }

.input:focus { border-bottom: 3px solid orange; outline: none; }

.input:nth-child(1) { cursor: pointer; pointer-events: all; }

JavaScript

// script.js const inputs = document.getElementById("inputs");

inputs.addEventListener("input", function (e) { const target = e.target; const val = target.value;

if (isNaN(val)) {
    target.value = "";
    return;
}

if (val != "") {
    const next = target.nextElementSibling;
    if (next) {
        next.focus();
    }
}

});

inputs.addEventListener("keyup", function (e) { const target = e.target; const key = e.key.toLowerCase();

if (key == "backspace" || key == "delete") {
    target.value = "";
    const prev = target.previousElementSibling;
    if (prev) {
        prev.focus();
    }
    return;
}

});

`

**Output: