Skip to content Skip to sidebar Skip to footer

TextArea Maximum Length?

Is it possible to set the maximum length of text in a TextArea?

Solution 1:

Something interesting is that HTML5 have added the feature of maxlength to textarea, if HTML5 is something you are ok to use right now.

W3C official documentation

Demo:

<textarea maxlength="20"></textarea>

Solution 2:

You can use following format in HTML

<input type="text" maxlength="13">

Solution 3:

If you are using jQuery, use this plugin http://www.stjerneman.com/demo/maxlength-with-jquery


Solution 4:

This solution is re-usable for all text areas via one function and it doesn't inform the user that he/she is typing too many characters, it prevents them from doing so, sort of like maxlength

The Function:

<script language="javascript" type="text/javascript">
function imposeMaxLength(Object, MaxLen)
{
  return (Object.value.length <= MaxLen);
}
</script>

Implementation:

<textarea name="myName" onkeypress="return imposeMaxLength(this, 15);" ><textarea>

Solution 5:

The Textarea doesn't accept the maxlength.

I have created a javascript function to handle this on onchange event. On one of my solutions I avoid the submit on form onsubmit event.

The code bellow will avoid submit if the textarea has more than 255 caracters

<script>
function checkSize(){
    var x = document.getElementById('x');
    return x.value.length <= 255;
}
</script>
<form onsubmit="return checkSize()">
    <textarea id="x"><textarea>
</form>

Post a Comment for "TextArea Maximum Length?"