type="number" vs inputmode="numeric"
A number field is for amounts you count up and down. Everything else made of digits is text.
The difference in one line: type="number" changes how the browser stores and checks the value, while inputmode="numeric" only changes the keyboard.
What goes wrong with type="number"
- Leading zeros vanish. ZIP code 02134 and account number 0042 lose their zeros when the value is read as a number.
- Scrolling changes the value. On a computer, scrolling the page with the pointer over a focused number field can step its value up or down.
- "e" is allowed. 1e5 is a valid number, so the letter e passes while other letters are thrown away.
- Long values round. A 16-digit card number is too long to be stored exactly as a JavaScript number.
- Invalid input turns into nothing. If someone types something that is not a number, the field's value is an empty string, so your code cannot even show them what went wrong.
The GOV.UK Design System team moved away from <input type="number"> to <input type="text" inputmode="numeric" pattern="[0-9]*"> for these reasons. Their notes also say iOS shows the full keyboard for type="number", and that pattern="[0-9]*" was what triggered the number pad there.
The pattern to copy
<label for="zip">ZIP code</label>
<input id="zip" name="zip" type="text"
inputmode="numeric" pattern="[0-9]*"
autocomplete="postal-code" maxlength="5">
inputmode opens the number pad on iOS 13 and later and on Android. pattern covers older iPhones and gives you free validation. autocomplete lets the phone fill the value in.
When type="number" is right
Use it for real quantities: how many tickets, a person's age, the number of rooms. These are values where a step of one makes sense and a spinner or arrow keys help. Even then, think about the iPhone keyboard. type="number" opens the full keyboard on its 123 page, which has a minus key. If you add inputmode="numeric" to it you get the bigger number pad, but you lose the minus key and the decimal point.
Decimals
For prices or weights use inputmode="decimal". It adds a point to the pad, or a comma in regions that write decimals with one. Accept both when you read the value. If the value can be negative, do not use either pad on iPhone: neither has a minus key.
Quick decision table
| Value | Use |
|---|---|
| Quantity, age, count | type="number" |
| ZIP code (5 digits), OTP, card number, CVC | type="text" inputmode="numeric" |
| Price, weight, percentage | type="text" inputmode="decimal" |
| Can be negative | type="text" (standard keyboard) |
| Phone number | type="tel" |
| ZIP+4, UK or Canadian postcode | type="text" |
Frequently asked questions
Does inputmode="numeric" validate the value?
No. It only chooses the keyboard. Add pattern="[0-9]*" or check the value in your code.
Why not just use type="tel" for every digit field?
The phone pad works, but screen readers announce the field as a phone number, and the + * # key is confusing on a ZIP or card field.
Found a mistake or want a check added? Write to contact (at) mobileformcheck.com.