Categories
HTML

How to make HTML text input allow only numeric input?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *