问题
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