How to set a maximum character limit for an input or textarea tag in HTML?

Published January 1, 2021

To set a maximum number of characters on an input or textarea tag in HTML, you can use the maxlength attribute on either of those tags and pass the maximum numbers of characters it should take as an integer value to it.

For example, let's say we have an input tag of type text and we want to limit the characters to 30. We can achieve this functionality like this,

<input type="text" maxlength="30" />

You can also pass the maxlength attribute to a textarea tag to limit characters like this,

<textarea maxlength="100"></textarea>

See the above examples live in JSBin.

Feel free to share if you found this useful 😃.

Share on: Facebook Twitter