Kotak masukan OTP 6 digit. Memasukkan nomor secara otomatis berpindah ke kotak berikutnya.
Aliran autentikasi: Ideal untuk input kode autentikasi SMS/email, layar autentikasi dua faktor (2FA).
Dukungan tempel: Menempelkan nomor dari clipboard akan secara otomatis mendistribusikannya ke setiap kotak.
Pengoptimalan seluler: inputmode="numeric" secara otomatis menampilkan keyboard numerik pada ponsel cerdas.
Validasi: Kecualikan input non-numerik dengan replace(/[^0-9]/g, '').
Ukuran kotak 52x60 piksel: Ukuran optimal untuk visibilitas tinggi dan pengoperasian sentuh yang mudah.
transform: scale(1.05) pada fokus: Anda dapat melihat dengan jelas kotak mana yang sedang dimasukkan.
.filled class: Visualisasikan kemajuan dengan mengubah warna batas kotak yang terisi.
{/* Kurangi elemen masukan menjadi 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" />
.otp-box { width: 44px; height: 52px; font-size: 1.25rem; }
<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> | Peramban | Status kompatibilitas |
|--------|-------|
| Chrome 66+ | ✅ Sepenuhnya kompatibel |
| Firefox 63+ | ✅ Sepenuhnya kompatibel |
| Safari 12+ | ✅ Sepenuhnya kompatibel |
| Tepi 79+ | ✅ Sepenuhnya kompatibel |
Di perangkat seluler,
inputmode="numeric"secara otomatis menampilkan keyboard numerik, sehingga sangat meningkatkan kegunaan.