I keep getting strange unexpected validation errors with my HTML5 number field. This is my HTML code:
<input type="number" name="width" maxlength="5" placeholder="Width" value="733.95591182365" /> Width<br />
When I enter 500 and submit the form, I receive the error: "Please enter a valid value. The two nearest valid values are 499.95591182365 and 500.95591182365."
I've solved the problem. The problem was this value="733.95591182365"
. I've altered my PHP code to round decimals to integers so that only integers can be echoed into the number field. Now I only receive errors when decimals are entered, which is OK.
<input type="number"> <input> elements of type number are used to let the user enter a number.
The <input type="range"> defines a control for entering a number whose exact value is not important (like a slider control). Default range is 0 to 100. However, you can set restrictions on what numbers are accepted with the attributes below. Tip: Always add the <label> tag for best accessibility practices!
Input value length If you try to submit the form with less than 4 characters, you'll be given an appropriate error message (which differs between browsers). If you try to enter more than 8 characters, the browser won't let you.
You need to have step="any"
as an attribute when working with type="number"
<form>
<input step="any" type="number" name="width" maxlength="5" placeholder="Width" value="733.95591182365" />
</form>
For number inputs,
The step scale factor is 1. The default step is 1
Then, since there is no step
attribute, the allowed value step is 1×1 = 1
:
- If the attribute is absent, then the allowed value step is the default step multiplied by the step scale factor.
And the step base will be 733.95591182365
:
- If the element has a
value
content attribute, and the result of applying the algorithm to convert a string to a number to the value of thevalue
content attribute is not an error, then return that result and abort these steps.
Therefore,
Constraint validation: When the element has an allowed value step, and the result of applying the algorithm to convert a string to a number to the string given by the element's value is a number, and that number subtracted from the step base is not an integral multiple of the allowed value step, the element is suffering from a step mismatch.
Since 733.95591182365 - 500 = 233.95591182365
is not a multiple of 1
, that constraint is violated.
If you want another step, specify a step
attribute. If you don't want any step, use step = "any"
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With