How to fix maxlength being ignored for HTML input type=”number” in Chrome?

Spread the love

Sometimes, we want to fix maxlength being ignored for HTML input type="number" in Chrome.

In this article, we’ll look at how to fix maxlength being ignored for HTML input type="number" in Chrome.

How to fix maxlength being ignored for HTML input type="number" in Chrome?

To fix maxlength being ignored for HTML input type="number" in Chrome, we add the pattern attribute.

For instance, we write

<input type="text" pattern="\d*" maxlength="4" />

to set the pattern attribute to a regex that validates that the input is a number in a text input to enforce validation.

Conclusion

To fix maxlength being ignored for HTML input type="number" in Chrome, we add the pattern attribute.

Leave a Reply

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