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 to a regex.

For instance, we write

<input id="numbersOnly" pattern="[0-9.]+" type="text" />

to set the pattern attribute to a regex that matches numbers.

Then we can only enter numbers without getting a validation error.

Conclusion

To make HTML text input allow only numeric input, we set the pattern attribute to a regex.

Leave a Reply

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