Reputation: 830
I have a <input type="number"></input>
field and when I try to put in a decimal as the first character, it comes up as invalid (I have an ng-change firing).
.5
won't work, but 0.5
is valid. Is there something I can do about this?
Upvotes: 3
Views: 451
Reputation: 88086
Per the HTML spec, .5
is valid for <input type="number">
.
So, you’re right, and the tool (browser? Angular?) that validation error’s originating from is wrong.
As far as how to deal with it—how to work around it—I don’t know what to suggest, but as someone who actually works on the specs for this stuff, I would like to ask you to please at least file a bug against whatever tool is (mis)performing the actual validation that’s causing you to see that message. If nobody takes time to report spec-conformance bugs like this (but instead everybody works around it by just putting, e.g., 0.5
to get past it), then the bugs never get fixed.
Anyway as far as evidence for my assertion that .5
is in fact valid: The HTML spec is pretty clear on this; see the section defining what a valid floating-point number is:
A string is a valid floating-point number if it consists of:
- Optionally, a U+002D HYPHEN-MINUS character (-).
- One or both of the following, in the given order:
- A series of one or more ASCII digits.
- Both of the following, in the given order:
- A single U+002E FULL STOP character (.).
- A series of one or more ASCII digits.
- Optionally:
- Either a U+0065 LATIN SMALL LETTER E character (e) or a U+0045 LATIN CAPITAL LETTER E character (E).
- Optionally, a U+002D HYPHEN-MINUS character (-) or U+002B PLUS SIGN character (+).
- A series of one or more ASCII digits.
Along with that evidence from the spec itself, here’s a record of other supporting evidence: There was in fact a time when the HTML spec didn’t allow .5
but instead required it to be written as 0.5
; however, after a “Floating point numbers beginning with a dot should be valid and parsed correctly” bug was raised against the spec, the spec was subsequently changed (in 2011) to state what it currently states (that is, too allow, e.g., .5
).
So, any tool that’s flagging .5
as an error likely has not been updated in this regard since 2011, and so it regardless is in need of its maintainer(s) to go back into their code & evaluate their code against the current spec requirements, to make sure they are conforming to the current spec.
I hope the above provides enough ammunition to use in raising a bug against the responsible tool.
Upvotes: 4
Reputation: 511
If you want all the input numbers to be valid then you can set in your input field step to "any". It works all integers and decimals numbers. Like -
<input type="number" step="any" />
Upvotes: 0