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
autocomplete="one-time-code"tells the phone this field wants the code from a recent text message. On iPhone the code appears as a suggestion above the keyboard; one tap fills it.inputmode="numeric"opens the number pad. Usetype="text", nottype="number": codes can start with 0, and a number field drops it.pattern="[0-9]*"shows the number pad on older iPhones and gives basic validation.maxlengthstops extra digits, which helps when people paste.
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
- Do not disable paste on the field.
- Do not submit as soon as the sixth digit lands without letting people check it, unless the code is verified instantly and errors are explained.
- Do not time out the page without warning; give a "send a new code" link.
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.