I think what's missing in the answers above is the need to specify a different value for the step
attribute, which has a default value of 1
. If you want the input's validation algorithm to allow floating-point values, specify a step accordingly.
For example, I wanted dollar amounts, so I specified a step like this:
<input type="number" name="price"
pattern="[0-9]+([.,][0-9]+)?" step="0.01"
title="This should be a number with up to 2 decimal places.">
There's nothing wrong with using jQuery to retrieve the value, but you will find it useful to use the DOM API directly to get the elements's validity.valid
property.
I had a similar issue with the decimal point, but the reason I realized there was an issue was because of the styling that Twitter Bootstrap adds to a number input with an invalid value.
Here's a fiddle demonstrating that the adding of the step
attribute makes it work, and also testing whether the current value is valid:
TL;DR: Set the a step
attribute to a floating-point value, because it defaults to 1
.
NOTE: The comma doesn't validate for me, but I suspect that if I set my OS language/region settings to somewhere that uses a comma as the decimal separator, it would work. *note in note*: that was not the case for OS language/keyboard settings *German* in Ubuntu/Gnome 14.04.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…