One-time code input that fills itself

With two attributes, an iPhone offers the code from Messages right above the keyboard.

<label for="otp">Verification code</label>
<input id="otp" name="otp" type="text"
       inputmode="numeric" pattern="[0-9]*"
       autocomplete="one-time-code" maxlength="6">

What each part does

One box, not six

Six separate one-digit boxes look neat but break easily. Autofill and paste put the whole code into the first box. Screen readers announce six unlabeled fields. Moving focus between them needs JavaScript that often fights the keyboard. If you want the six-box look, draw it with CSS on a single input, for example with letter-spacing and a background pattern.

Make the message match

For iPhone to offer the code, the text message has to contain it clearly, ideally with the word "code". You can also add an origin-bound line at the end, such as @example.com #123456, so the code is only offered on your domain. This format is supported by Safari and by Chrome on Android.

Do not

Frequently asked questions

Why does my code not appear above the keyboard?

Check that the field has autocomplete="one-time-code", that only one field on the page has it, and that the message clearly contains the code.

Should a one-time code field use type="number"?

No. Use type="text" with inputmode="numeric", so leading zeros are kept.

Found a mistake or want a check added? Write to contact (at) mobileformcheck.com.