Number inputs accept e, plus, minus and dots, and handle codes like PINs badly. Learn when to use a text input with inputmode and pattern instead, and when number is still right.
The short answer
#type="number" is meant for quantities, not for any value made of digits. Browsers let people type e, +, - and . because those are valid in numbers like 1e3 or -2.5, and some browsers accept any character and simply report an empty value. For things like PINs, card numbers, postcodes and verification codes, use a text input with inputmode and pattern.
_10<label for="code">Verification code</label>_10<input_10 id="code"_10 name="code"_10 type="text"_10 inputmode="numeric"_10 pattern="[0-9]{6}"_10 autocomplete="one-time-code"_10 required_10/>
Why it happens
#A number input parses whatever is typed as a floating point number. The characters e, E, +, - and . are all part of that syntax, so the browser cannot block them while someone is still typing. Chromium browsers block most other letters, but Firefox lets people type anything and only checks the value afterwards.
When the text does not parse, the browser does not give you the bad text. The value property becomes an empty string and validity.badInput becomes true. That is why a form can seem to "lose" what someone typed.
Number inputs also do things you rarely want for codes:
- Leading zeros can be lost once the value is treated as a number, so
007turns into7. - Long values such as card numbers go past the safe integer range and lose precision.
- Spin buttons and, in some browsers, the arrow keys or scroll wheel change the value by accident.
- Screen readers announce it as a spin button, which is confusing for a postcode.
A good rule: if adding one to the value makes no sense, it is not a number.
How to fix it
#Use inputmode for digit-only values
inputmode="numeric" tells mobile browsers to show a digit keypad while keeping the field a normal text input. The value stays a string, so leading zeros and long values are safe.
_10<label for="pin">PIN</label>_10<input id="pin" type="text" inputmode="numeric" pattern="[0-9]{4}" />
Use inputmode="decimal" when people need a decimal point too, for example a price field.
_10<label for="amount">Amount</label>_10<input id="amount" type="text" inputmode="decimal" pattern="[0-9]+([.,][0-9]{1,2})?" />
Add pattern for validation
pattern is a regular expression that must match the whole value. You do not need ^ or $, because the browser anchors it for you. When it does not match, the field is invalid, the form will not submit, and the :invalid CSS pseudo-class applies. Pair it with a visible hint or a title that explains the expected format.
You can also check it in JavaScript before sending data:
_10const input = document.querySelector('#code');_10_10if (!input.checkValidity()) {_10 input.reportValidity();_10}
Keep type=number for real quantities
For values people count or measure, such as a quantity in a basket, an age or a number of guests, type="number" is still a good choice. Set min, max and step so the browser can validate the range.
_10<label for="qty">Quantity</label>_10<input id="qty" type="number" min="1" max="20" step="1" value="1" />
Read the value with valueAsNumber, which gives you a number or NaN, and check validity.badInput if you need to tell "empty" apart from "typed something invalid".
_10const qty = document.querySelector('#qty');_10_10if (qty.validity.badInput) {_10 console.log('Not a number');_10} else {_10 console.log(qty.valueAsNumber);_10}
Always validate on the server
Browser validation is a convenience for users. Anyone can send any value to your server, so check the format again there.
Common pitfalls
#- Using
maxlengthon a number input. It is ignored fortype="number"; usemaxor switch to a text input. - Stripping letters with a keydown handler. It breaks paste, autofill and assistive technology. Let people type, then validate.
- Using
pattern="\d*"and forgetting that an empty value still matches. Addrequiredif the field must be filled in. - Relying on
patternalone to get a number keypad. Modern mobile browsers useinputmodefor that. - Forgetting
autocomplete="one-time-code"on verification code fields, which lets phones offer the code from a text message.
Related reading
#Was this resource helpful?