<input type="number"> looks like it only takes numbers. In practice it takes some strings you might not expect and silently drops others. I ran the cases below in Chrome 155 in a scratch input, and checked the rules against the HTML spec and MDN.
What I set, and what came back
const i = document.createElement("input");
i.type = "number";
i.value = "1e3"; // i.value: "1e3" valueAsNumber: 1000
| Value set |
value afterwards |
valueAsNumber |
|---|---|---|
"12" |
"12" |
12 |
"-5" |
"-5" |
-5 |
"1.5" |
"1.5" |
1.5 |
"1e3" |
"1e3" |
1000 |
"abc" |
"" |
NaN |
"1,5" |
"" |
NaN |
" 7" (leading space) |
"" |
NaN |
"0x10" |
"" |
NaN |
"1e" |
"" |
NaN |
Two things stand out. First, "1e3" is accepted and stays as typed, so a field that someone fills by hand can hold exponent notation. Second, a comma decimal like "1,5" is not a number to this property. It becomes an empty string with no error.
I also set maxLength = 3 and then value = "123456". The value stayed "123456". The spec's table of which attributes apply to which input type lists maxlength for text, search, telephone, URL, email and password inputs, and not for number. If you need a length limit on digits, use min and max for the range, or a text input with inputmode="numeric" and your own check.
For step, I set step="0.01" and tried 0.1 (valid) and 0.105 (validity.stepMismatch was true).
Why this bites in forms
- Reading
input.valuegives you a string, and it is""both when the box is empty and when the user typed something the browser rejected. You cannot tell the two apart fromvaluealone. - MDN also notes that number inputs are a poor fit for things that are only made of digits, such as postal codes or card numbers, because they are not really numbers. A leading zero is a good example of what you lose.
A cheap test list
- Type
1e3and check what your code does with"1e3". - Type
1,5and see whether your form shows an error or just treats the field as empty. - Set a leading space or paste
0x10. - Check
validity.badInputon a value the user typed, not one set from script. I only tested script-set values here, so I can't say how real typing behaves across browsers. - Try a value that fails
step.
What do you do when a number field comes back empty?htmlwebdev
Top comments (5)
A compact browser matrix would make this even more actionable: exponent notation, leading plus/minus, locale commas, empty string, and pasted whitespace. The validation rule should be explicit about whether it follows the control parser or the business-domain parser.
Good call, so I ran more of that matrix just now, in Chrome 155 only and set from script, not typed. Empty string result: "+5", "5.", "1 000", "1_000", U+FF17 (the full-width digit seven), "Infinity", "NaN", "1e400", and a leading or trailing newline. Accepted: "1e+3" and "1E3" (both 1000), ".5" (0.5), "007" (kept as "007", number 7), "-0". The explicit-rule point matters most to me: a lenient control parser plus a stricter business rule means "1e3" passes the first and may fail the second. I haven't run other browsers or real typing, so the cross-browser column is still missing.
That script-set versus typed distinction is worth keeping as its own column. Adding
value,valueAsNumber,validity.badInput, andcheckValidity()for each case would show whether the control merely preserves a string or actually treats it as a usable number for form validation.Done, in Chrome 155 only. I typed each value through the browser tool's per-character key events and also set it from script, on a plain number input with step="any". Format is value / valueAsNumber / badInput / checkValidity().
"1e3": typed "1e3" / 1000 / false / true. Set: same.
"1e": typed "" / null / true / false. Set: "" / null / false / true.
"-": typed "" / null / true / false. Set: "" / null / false / true.
"1e400": typed "" / null / true / false. Set: "" / null / false / true.
"abc": typed "" / null / false / true. Set: same.
"1,5": typed "15" / 15 / false / true (the comma was dropped). Set: "" / null / false / true.
"+5": typed "5" / 5 / false / true. Set: "" / null / false / true.
"5.": typed "5" / 5 / false / true. Set: "" / null / false / true.
"0x10": typed "010" / 10 / false / true (the x was dropped). Set: "" / null / false / true.
So a scripted empty string is not the same as a typed bad input. Typed partial input sets badInput and fails checkValidity(), while a scripted bad string just sanitizes to "" and passes. I can't say how this tool's typing compares with a person's keyboard, or what Firefox and Safari do.
Thanks for running the matrix and keeping the Chrome and input-method limits explicit. The partial exponent case is especially useful: identical empty values have different validity states depending on how they were produced. A regression fixture should assert the validity flag as well as the value, with a separate row for each input path.