OTP Input
An intuitive 6-digit OTP input component that autofocuses when you enter a single character and returns to the previous box with backspace. Also supports paste.
Preview
6-digit OTP input box. Entering a number automatically moves to the next box.
Use cases and features
-
Authentication flow: Ideal for SMS/email authentication code input, two-factor authentication (2FA) screens.
-
Paste support: Pasting numbers from the clipboard will automatically distribute them to each box.
-
Mobile optimization:
inputmode="numeric"automatically displays the numeric keyboard on smartphones. -
Validation: Exclude non-numeric inputs with
replace(/[^0-9]/g, '').
Points of design and UX
-
52x60px box size: Optimal size for high visibility and easy touch operation.
-
transform: scale(1.05)on focus: You can clearly see which box is currently being entered. -
.filledclass: Visualize progress by changing the border color of the filled box.
How to customize
Changed to 4 digit OTP
{/* Reduce input elements to 4 */}
<input class="otp-box" type="text" inputmode="numeric" maxlength="1" />
<input class="otp-box" type="text" inputmode="numeric" maxlength="1" />
<input class="otp-box" type="text" inputmode="numeric" maxlength="1" />
<input class="otp-box" type="text" inputmode="numeric" maxlength="1" />
Change box size
.otp-box { width: 44px; height: 52px; font-size: 1.25rem; }
Implementation code
<div class="otp-inputs" id="otp-inputs">
<input class="otp-box" type="text" inputmode="numeric" maxlength="1" />
<input class="otp-box" type="text" inputmode="numeric" maxlength="1" />
<input class="otp-box" type="text" inputmode="numeric" maxlength="1" />
<input class="otp-box" type="text" inputmode="numeric" maxlength="1" />
<input class="otp-box" type="text" inputmode="numeric" maxlength="1" />
<input class="otp-box" type="text" inputmode="numeric" maxlength="1" />
</div>
<style>
.otp-inputs { display: flex; gap: 0.75rem; }
.otp-box {
width: 52px; height: 60px;
background: rgba(255,255,255,0.06);
border: 2px solid rgba(255,255,255,0.12);
border-radius: 12px;
color: white;
font-size: 1.5rem;
font-weight: 700;
text-align: center;
outline: none;
transition: border-color 0.2s, background 0.2s, transform 0.1s;
}
.otp-box:focus { border-color: #6366f1; background: rgba(99,102,241,0.1); transform: scale(1.05); }
.otp-box.filled { border-color: rgba(99,102,241,0.5); }
</style>
<script>
(function() {
var inputs = Array.from(document.querySelectorAll('#otp-inputs .otp-box'));
inputs.forEach(function(input, i) {
input.addEventListener('input', function(e) {
var val = e.target.value.replace(/[^0-9]/g, '');
e.target.value = val.slice(-1);
if (val && i < inputs.length-1) inputs[i+1].focus();
e.target.classList.toggle('filled', !!e.target.value);
});
input.addEventListener('keydown', function(e) {
if (e.key==='Backspace' && !e.target.value && i>0) {
inputs[i-1].focus(); inputs[i-1].value=''; inputs[i-1].classList.remove('filled');
}
});
input.addEventListener('paste', function(e) {
e.preventDefault();
var text = (e.clipboardData || window.clipboardData).getData('text').replace(/[^0-9]/g, '');
text.split('').forEach(function(ch, j) {
if (inputs[i+j]) { inputs[i+j].value = ch; inputs[i+j].classList.add('filled'); }
});
var nextEmpty = inputs.findIndex(function(inp, j) { return j >= i && !inp.value; });
if (nextEmpty !== -1) inputs[nextEmpty].focus();
});
});
})();
</script> Browser compatibility status
| Browser | Compatibility status |
|--------|-------|
| Chrome 66+ | ✅ Fully compatible |
| Firefox 63+ | ✅ Fully compatible |
| Safari 12+ | ✅ Fully compatible |
| Edge 79+ | ✅ Fully compatible |
On mobile,
inputmode="numeric"automatically displays the numeric keyboard, greatly improving usability.