Is there a float input type in HTML5?
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:
<label for="totalAmt">Total Amount</label>
<input type="number" step="0.01" id="totalAmt">
(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>
HTML 5 input type=number element for floating point numbers on Chrome
Try <input type="number" step="any" />
It won't have validation problems and the arrows will have step of "1"
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.The following range control only accepts values in the range 0..1, and
allows 256 steps in that range:
<input name=opacity type=range min=0 max=1 step=0.00392156863>
The
following control allows any time in the day to be selected, with any
accuracy (e.g. thousandth-of-a-second accuracy or more):
<input name=favtime type=time step=any>
Normally, time controls are
limited to an accuracy of one minute.
http://www.w3.org/TR/2012/WD-html5-20121025/common-input-element-attributes.html#attr-input-step
Allow 2 decimal places in input type=number
Instead of step="any"
, which allows for any number of decimal places, use step=".01"
, which allows up to two decimal places.
More details in the spec: https://www.w3.org/TR/html/sec-forms.html#the-step-attribute
How to input float numbers in HTML form?
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)
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!
Html attribute 'Max' with float value make problems
You need to add the step
property like this:
<form> <input type="number" max="21.1" step="0.1"/></form>
html5 input[type=number] two decimal points
No, you can't do this with input[type=number]
. Version numbers like 1.0.2 are not mathematically legal.
However, you can use input[type=text]
and define a proper regular expression as its pattern
attribute.
For example this one matches semantic versioning numbers:
<input type="text" pattern="\d+\.\d+\.\d+">
Related Topics
How to Force Child Div to Be 100% of Parent Div'S Height Without Specifying Parent'S Height
Equal Width Flex Items Even After They Wrap
Should Ol/Ul Be Inside ≪P≫ or Outside
Custom HTML Tag Attributes Are Not Rendered by Jsf
Why Is Form Enctype=Multipart/Form-Data Required When Uploading a File
Why the Content Is Not Covered by the Background of an Overlapping Element
Regular Expression to Remove HTML Tags from a String
Twitter Bootstrap 3, Vertically Center Content
How to Use Google Chrome 11'S Upload Folder Feature in My Own Code
Submit Form Using a Button Outside the ≪Form≫ Tag
How to Center Elements on the Last Row in CSS Grid
Base64 Encoded Images in Email Signatures
Does Form Data Still Transfer If the Input Tag Has No Name
Set Textview Text from Html-Formatted String Resource in Xml
How to Make This Arrow in CSS Only