开发者

TextArea Maximum Length?

开发者 https://www.devze.com 2023-02-13 04:29 出处:网络
Is it possible to set the maximum length of tex开发者_开发问答t in a TextArea? Something interesting is that HTML5 have added the feature of maxlength to textarea, if HTML5 is something you are ok to

Is it possible to set the maximum length of tex开发者_开发问答t in a TextArea?


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>


You can use following format in HTML

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


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


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>


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消