Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I enter 500 in a HTML5 number field?

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" />&nbsp;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.

like image 232
Dan Bray Avatar asked Mar 23 '16 00:03

Dan Bray


People also ask

How do you input a number in HTML?

<input type="number"> <input> elements of type number are used to let the user enter a number.

How do you set range in input type 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!

What is the minimum length a text field can be?

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.


2 Answers

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>
like image 63
Handonam Avatar answered Sep 30 '22 17:09

Handonam


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:

  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:

  1. 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 the value 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".

like image 20
Oriol Avatar answered Sep 30 '22 16:09

Oriol