<div class="otp-container">
<h3>Enter Verification Code</h3>
<div class="otp-inputs">
<input type="text" maxlength="1" class="otp-field" autofocus>
<input type="text" maxlength="1" class="otp-field">
<input type="text" maxlength="1" class="otp-field">
<input type="text" maxlength="1" class="otp-field">
</div>
</div>
.otp-container {
font-family: system-ui, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
text-align: center;
background: #fff;
padding: 30px;
border-radius: 16px;
box-shadow: 0 4px 15px rgba(0,0,0,0.05);
max-width: 350px;
}
.otp-container h3 {
margin: 0 0 20px 0;
color: #1f2937;
}
.otp-inputs {
display: flex;
gap: 10px;
justify-content: center;
}
.otp-field {
width: 50px;
height: 60px;
font-size: 1.5rem;
text-align: center;
border: 1px solid #d1d5db;
border-radius: 8px;
outline: none;
transition: border-color 0.2s;
font-weight: 600;
color: #1f2937;
}
.otp-field:focus {
border-color: #3b82f6;
box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}
const inputs = document.querySelectorAll(".otp-field");
inputs.forEach((input, index) => {
// Handle Typing
input.addEventListener("keyup", (e) => {
if (e.key >= 0 && e.key <= 9) {
if (index < inputs.length - 1) inputs[index + 1].focus();
} else if (e.key === "Backspace") {
if (index > 0) inputs[index - 1].focus();
}
});
// Handle Paste
input.addEventListener("paste", (e) => {
e.preventDefault();
const data = e.clipboardData.getData("text");
if (!/^\d+$/.test(data)) return;
const digits = data.split("");
inputs.forEach((inp, i) => {
if (i >= index && digits.length > 0) {
inp.value = digits.shift();
inp.focus();
}
});
});
});