Sometimes, we want to make HTML text input allow only numeric input.
In this article, we’ll look at how to make HTML text input allow only numeric input.
How to make HTML text input allow only numeric input?
To make HTML text input allow only numeric input, we set the pattern
attribute.
For instance, we write
<input id="numbersOnly" pattern="[0-9.]+" type="text" />
to set the pattern
attribute to a regex that validates that the input values are only digits or a decimal point.
Conclusion
To make HTML text input allow only numeric input, we set the pattern
attribute.