DEV Community

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

Posted on • Originally published at melvingeorge.me

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

Originally posted here!

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" />
Enter fullscreen mode Exit fullscreen mode

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

<textarea maxlength="100"></textarea>
Enter fullscreen mode Exit fullscreen mode

See the above examples live in JSBin.

Feel free to share if you found this useful 😃.


Top comments (0)