Sometimes, we want to handle floats and decimal separators with HTML input type number.
In this article, we’ll look at how to handle floats and decimal separators with HTML input type number.
How to handle floats and decimal separators with HTML input type number?
To handle floats and decimal separators with HTML input type number, we can set the pattern and step attributes.
For instance, we write
<input type="number" name="price" pattern="[0-9]+([\.,][0-9]+)?" step="0.01" />
to add a number input.
We set its pattern attribute to a pattern that we use to validate that the number entered is a decimal number.
And we set the step attribute to 0.01 to keep the number entered to 2 decimal places.
Conclusion
To handle floats and decimal separators with HTML input type number, we can set the pattern and step attributes.