<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><script language=javascript> <BR><BR> <br><script language=javascript> <br><BR> <BR><script language=javascript> <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>} <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; </script> </div>中斷; <br>} <br>tempValue=tt; <br> window.clipboardData.setData("Text", tempValue); } window.event.returnValue = true; } //--> 然後設定多行的文字方塊或文字區域的2 個屬性。 onkeypress="javascript:setMaxLength(this,100);" onpaste="limitPaste(this, 100)" 現在好了,可以自動區分中英文了,這個方案不錯,供大家分享