Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a float input type in HTML5?

According to html5.org, the "number" input type's "value attribute, if specified and not empty, must have a value that is a valid floating point number."

Yet it is simply (in the latest version of Chrome, anyway), an "updown" control with integers, not floats:

<input type="number" id="totalAmt"></input>

Is there a floating point input element native to HTML5, or a way to make the number input type work with floats, not ints? Or must I resort to a jQuery UI plugin?

like image 778
B. Clay Shannon-B. Crow Raven Avatar asked Sep 25 '13 17:09

B. Clay Shannon-B. Crow Raven


People also ask

What is the input type for float in HTML?

According to html5.org, the "number" input type's "value attribute, if specified and not empty, must have a value that is a valid floating point number." Yet it is simply (in the latest version of Chrome, anyway), an "updown" control with integers, not floats: <input type="number" id="totalAmt"></input>

How do you do a float in HTML?

For any floating-point numbers, use type="number”, as it is widely supported and can also help to prevent random input. You can use the “any” value with step to allow any number of decimal places. Let's see an example to understand how various steps affect different input types.

What is float value in HTML?

The float CSS property places an element on the left or right side of its container, allowing text and inline elements to wrap around it. The element is removed from the normal flow of the page, though still remaining a part of the flow (in contrast to absolute positioning).


1 Answers

The number type has a step value controlling which numbers are valid (along with max and min), which defaults to 1. This value is also used by implementations for the stepper buttons (i.e. pressing up increases by step).

Simply change this value to whatever is appropriate. For money, two decimal places are probably expected:

<input type="number" step="0.01"> 

(I'd also set min=0 if it can only be positive)

If you'd prefer to allow any number of decimal places, you can use step="any" (though for currencies, I'd recommend sticking to 0.01). In Chrome & Firefox, the stepper buttons will increment / decrement by 1 when using any. (thanks to Michal Stefanow's answer for pointing out any, and see the relevant spec here)

Here's a playground showing how various steps affect various input types:

<form>    <input type=number step=1 /> Step 1 (default)<br />    <input type=number step=0.01 /> Step 0.01<br />    <input type=number step=any /> Step any<br />    <input type=range step=20 /> Step 20<br />    <input type=datetime-local step=60 /> Step 60 (default)<br />    <input type=datetime-local step=1 /> Step 1<br />    <input type=datetime-local step=any /> Step any<br />    <input type=datetime-local step=0.001 /> Step 0.001<br />    <input type=datetime-local step=3600 /> Step 3600 (1 hour)<br />    <input type=datetime-local step=86400 /> Step 86400 (1 day)<br />    <input type=datetime-local step=70 /> Step 70 (1 min, 10 sec)<br />  </form>

As usual, I'll add a quick note: remember that client-side validation is just a convenience to the user. You must also validate on the server-side!

like image 80
Dave Avatar answered Sep 19 '22 19:09

Dave