Better One-Time Code Inputs Without Six Tiny Boxes
A common verification screen shows six small input boxes:
[1] [2] [3] [4] [5] [6]
One labelled input usually handles this more simply. Separate boxes need extra code for:
- focus movement;
- Backspace behaviour;
- paste;
- mobile autofill;
- keyboard navigation;
- screen reader labels.
A simple code field
<label for="code">
Verification code
</label>
<input
id="code"
name="code"
type="text"
inputmode="numeric"
autocomplete="one-time-code"
maxlength="6"
pattern="[0-9]{6}">
Keep leading zeroes and offer a numeric keyboard
The value is still text:
type="text"
That matters because a code such as:
004281
is not a number you want JavaScript or the browser to turn into:
4281
But:
inputmode="numeric"
asks mobile browsers for a number-friendly keyboard.
Support autofill and paste
This tells the browser and operating system what the field represents. On supported phones, a verification code received through a message may be offered above the keyboard. The user can fill the whole code with one tap.
That is much better than manually moving through six fields. The user can copy:
582104
and paste it directly into the input. No custom paste listener is needed. This matters because many people copy verification codes from email, password managers, or messaging apps.
Why six inputs are harder
A six-field design often needs code like:
type one digit
↓
move focus right
↓
Backspace
↓
move focus left
↓
paste six digits
↓
distribute digits across fields
You are rebuilding text-input behaviour. A single field already handles all of that.
You can still make it look like a code
The single input does not have to look like a normal wide form field.
.code {
width: 10ch;
font-family: ui-monospace, monospace;
font-size: 1.5rem;
letter-spacing: 0.35em;
text-align: center;
}
Now the digits are visually separated without being separate controls. If the spacing is large, the final digit may create extra visual space. Adjust width and padding by eye.
The exact style is less important than keeping the underlying control simple.
Set the length and validate the code
maxlength="6"
prevents the field from accepting more than six characters. That is useful for a fixed-length code.
pattern="[0-9]{6}"
can tell the browser that exactly six digits are expected. Server-side validation still matters. Never trust the client-side pattern as your only check.
Tell users what to enter
A label such as:
Code
is okay. This is clearer:
Enter the 6-digit verification code
If the code expires, explain that nearby.
Do not disable paste
Some security interfaces do this:
event.preventDefault();
on paste events. That makes users manually retype a code and does not meaningfully improve security in most normal flows. Let people paste.
Keep the field accessible
A single input is simple for assistive technology. Six fields require six useful labels or a carefully designed group. That can be done correctly, but it is more work.
If the visual design does not provide a real benefit, I would rather use one labelled input.
Keep separate boxes only when they help
Separate boxes can work when paste, focus, and autofill have been handled properly. Compare that extra code with a single input:
<input
inputmode="numeric"
autocomplete="one-time-code">
Use one labelled input unless the separate boxes provide a benefit worth the extra interaction code.