TextArea Maximum Length?

不羁岁月 提交于 2019-12-23 13:24:11

问题


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


回答1:


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




回答2:


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>



回答3:


You can use following format in HTML

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



回答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>



回答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>


来源:https://stackoverflow.com/questions/5116564/textarea-maximum-length

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!