One-time code input that fills itself
With two attributes, an iPhone offers the code from Messages right above the keyboard.
Last updated November 30, 2026 · Keyboards checked on real phones: iPhone (iOS 17 and 18), Galaxy S24 and S25, Pixel 9, Motorola Edge 50 Fusion
<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.
What people actually type
On real phones, the keyboard for this field is not identical everywhere. The iPhone number pad has only digits. The Galaxy number pad adds a ".-" key and a comma, and Gboard on Pixel and Motorola adds a dash, comma, point and space. People who read a code like "123 456" from an email will type the space on Android. Strip everything that is not a digit before you check the code, rather than rejecting it.
otp.addEventListener('input', () => {
otp.value = otp.value.replace(/\D/g, '').slice(0, 6);
});
A message that autofills
Your Example verification code is 482913.
@example.com #482913
The first line is for people. The last line binds the code to your domain: Safari offers it only on example.com, and Chrome on Android can read it through the WebOTP API with navigator.credentials.get({ otp: { transport: ['sms'] } }). Keep the last line exactly in that format, with the domain of the page that shows the field.
Errors and resends
- Say what went wrong: "That code has expired. We sent a new one to •••• 0199."
- Keep the field filled so people can see what they typed.
- Show when a new code can be sent, for example after 30 seconds, instead of a disabled button with no explanation.
- Let people change the phone number without starting over.
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.
Does autocomplete="one-time-code" work on Android?
Chrome on Android can suggest or read the code too, especially when the message ends with the @domain #code line.
Found a mistake or want a check added? Write to contact (at) mobileformcheck.com.