Forms and inputs · mid
OTP input
An OTP input keeps one array of digits as the single source of truth, moves focus forward on input and back on Backspace, fills every box from a paste, accepts only digits, and uses the right mobile attributes so SMS autofill works.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- State: digits array of length n (or one string); an array of refs to the inputs; derive 'complete' and call onComplete once.
- Input: keep only digits; set the digit, move focus to the next box; typing in a filled box replaces it; if several characters arrive at once (autofill), treat it like a paste.
- Backspace: if the current box has a digit, clear it; if it's empty, clear the previous box and move focus back.
- Paste: take the digits from the clipboard, fill from the current box, focus the next empty box (or the last box).
- Arrow Left/Right, Home/End move between boxes; select the content on focus so typing replaces it.
- Mobile: inputMode='numeric' (not type='number'), autocomplete='one-time-code' (iOS SMS autofill; Android Chrome also has the WebOTP API), maxLength.
- Accessibility: a group label ('Enter the 6-digit code'), each input aria-label='Digit 2 of 6'; announce errors; a resend timer from the server's cooldown.
- Alternative many teams use: one real input (for paste/autofill) with six boxes drawn on top.
The red pen
Common traps: mistake → fix
Mistake: type='number'
Fix: allows 'e' and '+', scroll changes the value, drops leading zeros.
Mistake: Forget paste
Fix: the most common thing candidates skip.
Mistake: Read the digit from stale state in the handler
Fix: fast typing loses characters; use the event's value.
Easy to hard
Questions interviewers ask, easy to hard
- Build a 6-digit OTP input.
- Handle paste of the whole code.
- What happens on Backspace in an empty box?
- Make SMS autofill work on phones.
Practise
Practise with these questions
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.