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
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>
Why does the html input with type number allow the letter 'e' to be entered in the field?
Because that's exactly how the spec says it should work. The number input can accept floating-point numbers, including negative symbols and the e
or E
character (where the exponent is the number after the e
or E
):
A floating-point number consists of the following parts, in exactly
the following order:
- Optionally, the first character may be a "
-
" character.- One or more characters in the range "
0—9
".- Optionally, the following parts, in exactly the following order:
- a "
.
" character- one or more characters in the range "
0—9
"- Optionally, the following parts, in exactly the following order:
- a "
e
" character or "E
" character- optionally, a "
-
" character or "+
" character- One or more characters in the range "
0—9
".
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
HTML5 Number Input - Always show 2 decimal places
Solved following the suggestions and adding a piece of jQuery to force the format on integers:
parseFloat($(this).val()).toFixed(2)
HTML5 input box with type=number does not accept comma in Chrome browser
As of now (30/08/2017), Antoine Thiry's answer seems to be no longer valid in Chrome (my version is 60.0.3112.113). Unfortunately I don't have any other suggestion, other than simulating type="number" with javascript.
How to make HTML input tag only accept numerical values?
HTML 5
You can use HTML5 input type number to restrict only number entries:
<input type="number" name="someid" />
This will work only in HTML5 complaint browser. Make sure your html document's doctype is:
<!DOCTYPE html>
See also https://github.com/jonstipe/number-polyfill for transparent support in older browsers.
JavaScript
Update: There is a new and very simple solution for this:
It allows you to use any kind of input filter on a text
<input>
,
including various numeric filters. This will correctly handle
Copy+Paste, Drag+Drop, keyboard shortcuts, context menu operations,
non-typeable keys, and all keyboard layouts.
See this answer or try it yourself on JSFiddle.
For general purposes, you can have JS validation as below:
function isNumberKey(evt) {
var charCode = (evt.which) ? evt.which : evt.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
<input name="someid" type="number" onkeypress="return isNumberKey(event)" />
How can I set max-length in an HTML5 input type=number element?
And you can add a max
attribute that will specify the highest possible number that you may insert
<input type="number" max="999" />
if you add both a max
and a min
value you can specify the range of allowed values:
<input type="number" min="1" max="999" />
The above will still not stop a user from manually entering a value outside of the specified range. Instead he will be displayed a popup telling him to enter a value within this range upon submitting the form as shown in this screenshot:
Related Topics
Why Is 'Font-Family' Not Inherited in '<Button>' Tags Automatically
Why Everything Word-Wrap Inside an Absolute Element Nested Inside a Float or Inline-Block Element
Table's Border-Radius Does Not Function as Expected
How to Render a String with Jsx in React
Page-Break-Inside Doesn't Work in Chrome
To "User-Scalable=No" or Not to "User-Scalable=No"
HTML Validation Error for Property Attribute
Position Fixed and Internet Explorer
Unwanted Border-Radius Corners Around Images in Brave/Chrome Browser
Does CSS Grid Have a Flex-Grow Function
Preserve Dynamically Changed HTML on Back Button
Website Response Time: Difference Between 'Load' and 'Finish'
How to Serve HTML File from Another Directory as Actionresult
Why People Minify Assets and Not The HTML
Can Excel Interpret a Cell as HTML
Is This Empty Space Actually Margin-Right Which Is Not Recognized by Dev Tools