One-Time Code Input
Six code boxes that auto-advance, handle paste and verify at the last digit (demo code 123456).
AI prompt
Design a six-box one-time code input for phone verification. Each box is a 44 by 52 pixel rounded square with a large centred digit, inputmode numeric, autocomplete one-time-code on the first box. Typing a digit moves focus to the next box, Backspace on an empty box moves back and clears, arrow keys move between boxes, pasting a six digit code fills all boxes. When all six digits are entered, verify: code 123456 turns the borders green and shows "Verified", anything else shakes the row, turns borders red and shows "Wrong code. Try again." with focus returning to the first box. Group with role group and aria-label "Verification code", each box labelled "Digit 1 of 6". Light minimal style, prefers-reduced-motion fallback. Plain HTML, CSS and JavaScript.About this pattern
Definition
A one-time code input splits a short numeric code into separate boxes. Typing moves focus forward, Backspace moves it back and pasting fills all boxes at once.
Purpose & UX principles
- Faster and clearer than a single field for 4 to 6 digit codes; supports autofill from SMS.
- Accept paste of the whole code, ignore non-digits and verify automatically after the last digit.
- Show clear error feedback and keep focus so people can correct without extra taps.
Interaction states
- Empty, partially filled, verifying, success ("Verified") and error (shake, red border). The code 123456 succeeds; anything else fails.
Motion specification
- Error shakes 4px for 300ms; success border turns green in 150ms.
- Reduced motion: no shake, only the red border and message.
Responsive behaviour
- Boxes are at least 44px wide on touch; they shrink evenly on narrow screens.
- Use inputmode numeric and autocomplete one-time-code so phones offer the SMS code.
Code
<div class="bx"><div class="row" id="row" role="group" aria-label="Verification code"></div><p class="msg" id="msg" aria-live="polite">Enter the 6-digit code we sent you. Try 123456.</p><button id="rs" type="button">Clear</button></div>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
.bx{display:grid;gap:10px;justify-items:center}
.row{display:flex;gap:7px}.row.shake{animation:sk .3s}
@keyframes sk{25%{transform:translateX(-4px)}75%{transform:translateX(4px)}}
.row input{width:40px;height:50px;text-align:center;font-size:22px;font-weight:600;border:1.5px solid var(--line);border-radius:10px;background:var(--surface);caret-color:var(--text);transition:border-color .15s}
.row input:focus{outline:none;border-color:var(--text);box-shadow:0 0 0 3px rgba(23,23,23,.1)}
.row.ok input{border-color:#1a8f4a}.row.bad input{border-color:var(--danger)}
.msg{font-size:12.5px;color:var(--muted);text-align:center;max-width:280px}.msg.ok{color:#1a8f4a}.msg.bad{color:var(--danger)}
#rs{height:26px;padding:0 14px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11.5px;cursor:pointer}
button:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
@media (prefers-reduced-motion:reduce){.row.shake{animation:none}.row input{transition:none}}const row=document.getElementById('row'),msg=document.getElementById('msg'),N=6,bs=[];
for(let i=0;i<N;i++){const e=document.createElement('input');e.inputMode='numeric';e.maxLength=1;e.setAttribute('aria-label','Digit '+(i+1)+' of '+N);if(i===0)e.autocomplete='one-time-code';else e.autocomplete='off';row.append(e);bs.push(e);
e.addEventListener('input',()=>{e.value=e.value.replace(/\D/g,'').slice(0,1);if(e.value&&i<N-1)bs[i+1].focus();check();});
e.addEventListener('keydown',ev=>{if(ev.key==='Backspace'&&!e.value&&i>0){bs[i-1].value='';bs[i-1].focus();reset();}else if(ev.key==='ArrowLeft'&&i>0){ev.preventDefault();bs[i-1].focus();}else if(ev.key==='ArrowRight'&&i<N-1){ev.preventDefault();bs[i+1].focus();}});
e.addEventListener('focus',()=>e.select());
e.addEventListener('paste',ev=>{ev.preventDefault();const d=(ev.clipboardData.getData('text')||'').replace(/\D/g,'').slice(0,N);d.split('').forEach((c,k)=>bs[k].value=c);bs[Math.min(d.length,N-1)].focus();check();});}
function reset(){row.classList.remove('ok','bad');msg.className='msg';}
function check(){const v=bs.map(x=>x.value).join('');if(v.length<N){reset();return;}
if(v==='123456'){row.className='row ok';msg.className='msg ok';msg.textContent='✓ Verified';}
else{row.className='row bad shake';msg.className='msg bad';msg.textContent='Wrong code. Try again.';setTimeout(()=>{row.classList.remove('shake');bs.forEach(x=>x.value='');bs[0].focus();},320);}}
document.getElementById('rs').addEventListener('click',()=>{bs.forEach(x=>x.value='');reset();msg.textContent='Enter the 6-digit code we sent you. Try 123456.';bs[0].focus();});One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>One-Time Code Input — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
.bx{display:grid;gap:10px;justify-items:center}
.row{display:flex;gap:7px}.row.shake{animation:sk .3s}
@keyframes sk{25%{transform:translateX(-4px)}75%{transform:translateX(4px)}}
.row input{width:40px;height:50px;text-align:center;font-size:22px;font-weight:600;border:1.5px solid var(--line);border-radius:10px;background:var(--surface);caret-color:var(--text);transition:border-color .15s}
.row input:focus{outline:none;border-color:var(--text);box-shadow:0 0 0 3px rgba(23,23,23,.1)}
.row.ok input{border-color:#1a8f4a}.row.bad input{border-color:var(--danger)}
.msg{font-size:12.5px;color:var(--muted);text-align:center;max-width:280px}.msg.ok{color:#1a8f4a}.msg.bad{color:var(--danger)}
#rs{height:26px;padding:0 14px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11.5px;cursor:pointer}
button:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
@media (prefers-reduced-motion:reduce){.row.shake{animation:none}.row input{transition:none}}
</style></head>
<body><div class="stage">
<div class="bx"><div class="row" id="row" role="group" aria-label="Verification code"></div><p class="msg" id="msg" aria-live="polite">Enter the 6-digit code we sent you. Try 123456.</p><button id="rs" type="button">Clear</button></div>
</div>
<script>
const row=document.getElementById('row'),msg=document.getElementById('msg'),N=6,bs=[];
for(let i=0;i<N;i++){const e=document.createElement('input');e.inputMode='numeric';e.maxLength=1;e.setAttribute('aria-label','Digit '+(i+1)+' of '+N);if(i===0)e.autocomplete='one-time-code';else e.autocomplete='off';row.append(e);bs.push(e);
e.addEventListener('input',()=>{e.value=e.value.replace(/\D/g,'').slice(0,1);if(e.value&&i<N-1)bs[i+1].focus();check();});
e.addEventListener('keydown',ev=>{if(ev.key==='Backspace'&&!e.value&&i>0){bs[i-1].value='';bs[i-1].focus();reset();}else if(ev.key==='ArrowLeft'&&i>0){ev.preventDefault();bs[i-1].focus();}else if(ev.key==='ArrowRight'&&i<N-1){ev.preventDefault();bs[i+1].focus();}});
e.addEventListener('focus',()=>e.select());
e.addEventListener('paste',ev=>{ev.preventDefault();const d=(ev.clipboardData.getData('text')||'').replace(/\D/g,'').slice(0,N);d.split('').forEach((c,k)=>bs[k].value=c);bs[Math.min(d.length,N-1)].focus();check();});}
function reset(){row.classList.remove('ok','bad');msg.className='msg';}
function check(){const v=bs.map(x=>x.value).join('');if(v.length<N){reset();return;}
if(v==='123456'){row.className='row ok';msg.className='msg ok';msg.textContent='✓ Verified';}
else{row.className='row bad shake';msg.className='msg bad';msg.textContent='Wrong code. Try again.';setTimeout(()=>{row.classList.remove('shake');bs.forEach(x=>x.value='');bs[0].focus();},320);}}
document.getElementById('rs').addEventListener('click',()=>{bs.forEach(x=>x.value='');reset();msg.textContent='Enter the 6-digit code we sent you. Try 123456.';bs[0].focus();});
</script>
</body></html>