An accessible one-time code input: one real field under the slots

Code inputs are often built as six separate boxes, one input each. That breaks what people rely on. Paste and SMS autofill put the whole code into the first box, where a one-character limit cuts it off unless extra script spreads it out, and a screen reader announces six unnamed fields. Keeping one real input and drawing the slots from its value avoids all of it.

Live example: Slot OTP, https://motionmart.store/p/latch/slot-otp

1. One input with the right attributes

<input
  inputmode="numeric"
  autocomplete="one-time-code"
  maxlength="6"
  pattern="[0-9]*"
  aria-label="6-digit code"
/>

2. Draw the slots from the value

Put the input over the row of slots so a tap anywhere focuses it, and mark the slots aria-hidden so assistive technology only meets the one field. On every input event, write each character of input.value into its slot and highlight the slot at input.value.length while the field has focus. The slots are a picture of the value; the input stays the single source of truth.

3. Clean what arrives

People paste codes with spaces or a label in front ("428 913", "Code: 428913"). Strip everything that is not a digit and cut to the code length on each input event, then check the code automatically once it is complete. Slot OTP does exactly this, and checks a full code after a short pause so the last digit is seen landing.

input.addEventListener('input', () => {
  input.value = input.value.replace(/\D/g, '').slice(0, 6);
  render();
  if (input.value.length === 6) setTimeout(check, 260);
});

4. Say what happened

Give the result a polite live region under the field, so screen readers hear "Checking…", "Verified" or "That code did not match. Try again." without losing focus. After a wrong code, clear the field so the next attempt starts empty; after a right one, disable it. A resend button sits below as a real button, not a link.

5. Motion that never blocks typing

Because the animation only reads the input value, it can be as lively as you like without getting in the way. In Slot OTP each digit drops into its slot and bounces, a pasted code cascades with each digit 50 ms after the one before, a wrong code shakes the row and pops the digits out, and a right one closes the gaps so the slots fuse into one pill with a tick. With reduced motion turned on, the transitions are cut to almost nothing.

Questions

Should an OTP input be one field or several?

One field. Several fields break paste, SMS autofill and screen readers unless a lot of extra script repairs them. Draw the boxes from one input value instead.

Which autocomplete value is for verification codes?

autocomplete="one-time-code". Combine it with inputmode="numeric" for the number pad.

Should I use type="number" for a code?

No. Use a text input with inputmode="numeric": type="number" shows spin buttons and can drop leading zeros.

Browse by use case