TIL the inputmode attribute changes the mobile keyboard without changing validation
I always used type="number" for things like PIN codes and order numbers, then fought with spinner arrows, scroll-wheel changes and leading zeros getting dropped.
Turns out inputmode="numeric" on a regular text input brings up the number keypad on most phones while keeping the value as a plain string. Pair it with a pattern if you want validation.
There's also inputmode="email", "tel", "url" and "decimal". What other small HTML attributes do you wish you'd learned about earlier?
Comments · 5
Good one.
autocomplete="one-time-code"is another small attribute worth knowing, since it lets phones suggest the SMS code on a verification field.Pair
inputmode="numeric"withpattern="[0-9]*"andautocomplete="one-time-code"for OTP fields. The mobile keyboard and autofill both behave much better.The leading-zero issue is a nice reminder that a number field is for quantities, not identifiers. If you wouldn't do arithmetic on it, it's text.
Does inputmode work on every mobile browser, or are there differences between iOS and Android?
Support is broad on current iOS and Android browsers, but the exact keyboard layouts differ a bit. Test on both, particularly for the decimal point.