the MaxLength property of textarea in the standard DHTML document does not work by default and only works when an event occurs: http://spiderscript.net/site/spiderscript/examples/ex_textarea_maxlength.aspBut it has and works in text <input type="text"Maxlength=" -">, how many characters cannot be entered in textarea. Method 1, if you only need to intercept the content of how many characters, you can: copy code code as follows:<textarea onkeyup="this.value = This.value.slice (0, +)"></textarea>or copy the code code as follows:<textarea onkeyup="this.value = this.value.substring (0, +)"></textarea>Method 2, copy the Code code as follows:<script type="Text/javascript">function Ismaxlength (obj) {varMlength=obj.getattribute? parseint (Obj.getattribute ("maxlength")) :"" if(Obj.getattribute && obj.value.length>mlength) Obj.value=obj.value.substring (0, Mlength)} </script> <textarea maxlength=" +"onkeyup="return Ismaxlength (this)"></textarea>This method uses a truncation method, and when entered into the last character, the cursor flashes when it is entered. But can be resolved using CTRL+c copied over the length of the problem, but if you copy it with the mouse is not still not. Method 3, this method directly determine the length of the input copy code code is as follows:<script language="JavaScript"Type="Text/javascript"> <!--function Imposemaxlength (Object, maxlen) {return(Object.value.length <maxlen); } -</script> <textarea Name="MyName"onkeypress="return Imposemaxlength (this,);"></textarea>This implementation does not display the cursor flicker problem when the input content is greater than 15 o'clock, but it does not resolve the length limitation of the copy, which can exceed the maximum length limit .return(Object.value.length <=maxlen); But I test that I can enter a character when I enter the number of bytes =maxlen, so I change it toreturn(Object.value.length <maxlen); Method 4, in fact, Method 4 is Method 2 and Method 3 based on further optimization. Objectively speaking Method 2 and Method 3 have only done part of the work copy code code as follows:<mce:script language="JavaScript"Type="Text/javascript"><!--function Textlen (x, y) {varThelength =x.value.length; Window.status=thelength+' of'+y+'maximum characters.'; } function Maxtext (x, y) {tempstr=X.valueif(tempstr.length>y) {X.value= Tempstr.substring (0, y); } textlen (x, y); } //--></mce:script><form name="MyForm"> <textarea name="Mytextarea"cols=" $"rows="3"Wrap="Virtual"onkeypress="return (THIS.VALUE.LENGTH<20)"onkeydown="Textlen (this,20)"onkeyup="Textlen (this,20)"onblur="Maxtext (this,20)">The above method adds the Onblur event on the original basis, which is mainly used to deal with the problem when the user does not use the input but the copy and paste method to complete the text transfer. is actually a combination of method 2 and Method 3. Here's how I added and used the MaxLength attribute for textarea and combined the results of the previous example:"Text/javascript">function ismaxlength (obj) {varMlength=obj.getattribute? parseint (Obj.getattribute ("maxlength")) :""if(Obj.getattribute && obj.value.length>mlength) alert ('This text box allows you to enter a maximum length of'+mlength+"characters, beyond which the content will be truncated") Obj.value=obj.value.substring (0, mlength)}function imposemaxlength (obj) {varMlength=obj.getattribute? parseint (Obj.getattribute ("maxlength")) :"" return(Obj.value.length <mlength);} </script>"MyForm"> <textarea maxlength="5"onkeypress="return Imposemaxlength (this)"Onblur="Ismaxlength (This)"></textarea></form></body>JavaScript code---------------------------------------------------------------------------------------------Copy the code code as follows:function Settextareamaxlength (controlId, length) {
JScript File for TextArea
Keep user from entering more than maxLength characters
function dokeypress (control, length) {
maxLength = length;
value = Control.value;
if (maxLength && value.length > MaxLength-1) {
Event.returnvalue = false;
MaxLength = parseint (maxLength);
}
}
Cancel default behavior
function Dobeforepaste (control, length) {
maxLength = length;
if (maxLength) {
Event.returnvalue = false;
}
}
Cancel default behavior and create a new paste routine
function Dopaste (control, length) {
maxLength = length;
value = Control.value;
if (maxLength) {
Event.returnvalue = false;
MaxLength = parseint (maxLength);
var OTR = Control.document.selection.createRange ();
var iinsertlength = maxlength-value.length + oTR.text.length;
var sData = window.clipboardData.getData ("Text"). substr (0, iinsertlength);
Otr.text = SData;
}
}
function Dodragenter (control, length) {
maxLength = length;
value = Control.value;
if (maxLength) {
Event.returnvalue = false;
}
}
function addevent (elm, Evtype, FN, usecapture) {
if (Elm.addeventlistener) {
Elm.addeventlistener (Evtype, FN, usecapture);
return true;
} else if (elm.attachevent) {
var r = elm.attachevent (' on ' + Evtype, FN);
return R;
} else {
elm[' on ' + evtype] = fn;
}
}
function Attacheventtextareabeforepaste (obj, length) {
return function () {
Dobeforepaste (obj, length)
}
}
function Attacheventtextareapaste (obj, length) {
return function () {
Dopaste (obj, length)
}
}
function attacheventtextareakeypress (obj, length) {
return function () {
Dokeypress (obj, length)
}
}
function Attacheventtextareadragenter (obj, length) {
return function () {
Dodragenter (obj, length);
}
}
var obj = document.getElementById (controlId);
Addevent (obj, ' keypress ', attacheventtextareakeypress (obj, length), null);
Addevent (obj, ' beforepaste ', attacheventtextareabeforepaste (obj, length), null);
Addevent (obj, ' paste ', attacheventtextareapaste (obj, length), null);
Addevent (obj, ' DragEnter ', attacheventtextareadragenter (obj, length), null);
}
-----------------------------------------------------------------------------------------------The HTML code copies the code code as follows:<asp:textbox id="textboxaddress"runat="Server"Width="200px"TextMode="MultiLine"height="113px"Maxlength="Ten"></asp:TextBox> <script language="JavaScript"Type="Text/javascript">Settextareamaxlength ('<%=textboxaddress.clientid%>',Ten); </script>
Go TextArea setting the maximum input value of the MaxLength property JS code