<div class="codetitle"> <span><a style="CURSOR: pointer" data="95782" class="copybut" id="copybut95782" onclick="doCopy('code95782')"><u>复制代码</u></a></span>代码如下:</div> <div class="codebody" id="code95782"> <br><script language="javascript"> <BR><!-- <br><br>String.prototype.len=function(){ <BR>return this.replace(/[^x00-xff]/g,"**").length; <BR>} <br><br>//设置多行TextBox的最大长度 <BR>function setMaxLength(object,length) <BR>{ <BR>var result = true; <BR>var controlid = document.selection.createRange().parentElement().id; <BR>var controlValue = document.selection.createRange().text; <BR>if (controlid == object.id && controlValue != "") <BR>{ <BR>result = true; <BR>} <BR>else if (object.value.len() >= length) <BR>{ <BR>result = false; <BR>} <BR>if (window.event) <BR>{ <BR>window.event.returnValue = 结果; <BR>返回结果; <BR>} <BR>} <br><br>//粘贴时检查多行文本框的最大长度 <BR>function limitPaste(object,length) <BR>{ <BR>var tempLength = 0; <BR>if(document.selection) <BR>{ <BR>if(document.selection.createRange().parentElement().id == object.id) <BR>{ <BR>tempLength = document.selection. createRange().text.len(); <BR>} <BR>} <BR>var tempValue = window.clipboardData.getData("Text"); <BR>tempLength = object.value.len() tempValue.len() - tempLength; <BR>if (tempLength > 长度) <BR>{ <BR>tempLength -= 长度; <BR>//alert(tempLength); <BR>//alert(tempValue); <BR>var tt=""; <BR>for(var i=0;i<tempValue.len()-tempLength;i ) <BR>{ <BR>if(tt.len()<(tempValue.len()-tempLength)) <BR>tt=tempValue.substr(0,i 1); <BR>否则<BR>中断; <BR>} <BR>tempValue=tt; <BR>window.clipboardData.setData("Text", tempValue); <BR>} <br><br>window.event.returnValue = true; <BR>} <br><br>//--> <BR></脚本> <BR></script> </div> <br>然后设置多行的文本框或文本区域的 2 个属性。 <br>onkeypress="javascript:setMaxLength(this,100);" onpaste="limitPaste(this, 100)" <br>现在好了,可以自动区分中英文了,这个方案不错,供大家分享