<!-- TWO STEPS TO INSTALL LIMIT TEXTAREA:  1.  Copy the coding into the HEAD of your HTML document
  2.  Add the last code into the BODY of your HTML document  --><!-- STEP ONE: Paste this code into the HEAD of your HTML document  --><HEAD><SCRIPT LANGUAGE="JavaScript">
<!-- Original:  Ronnie T. Moore -->
<!-- Web Site:  The JavaScript Source --><!-- Dynamic 'fix' by: Nannette Thacker -->
<!-- Web Site: http://www.shiningstar.net --><!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com --><!-- Begin
function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else 
countfield.value = maxlimit - field.value.length;
}
// End -->
</script>
</HEAD><!-- STEP TWO: Copy this code into the BODY of your HTML document  --><BODY><!-- textCounter() parameters are:  text field, the count field, max length --><center>
<form name=myform action="YOUR-SCRIPT.CGI">
<font size="1" face="arial, helvetica, sans-serif"> ( You may enter up to 125 characters. )<br/>
<textarea name=message wrap=physical cols=28 rows=4 onKeyDown="textCounter(this.form.message,this.form.remLen,125);" onKeyUp="textCounter(this.form.message,this.form.remLen,125);"></textarea>
<br/>
<input readonly type=text name=remLen size=3 maxlength=3 value="125"> characters left</font>
</form>
</center><p><center>
<font face="arial, helvetica" SIZE="-2">Free JavaScripts provided<br/>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center><p><!-- Script Size:  1.37 KB -->

解决方案 »

  1.   

    onpaste="javascript:return false;"
      

  2.   

    <script>
      function trimtext(tf,m){
    if(tf.value.length>m) tf.value=tf.value.substring(0,m);
      }
    </script>
    <input id="tt" value="cvb" onpropertychange="trimtext(this,8);">
      

  3.   

    //适用于有无中文字符串长度
    String.prototype.lenB = function(){return this.replace(/[^\x00-\xff]/g,"**").length;}
      

  4.   

    js中的字符无所谓单双字节,都是unicode字符,占两个字节。但占的宽度可能不同
      

  5.   

    to  syre(神仙) (
    ---------------
    我猜测楼主的“一定长度”是字符总宽度的意思,汉字相当于两个字符的宽度,这样maxlength就不行了。
    要是这样,可以用下面的办法:
    <script>
      function trimtext(tf,m){
        var newvalue=trimstr(tf.value,m);
    if(newvalue!=tf.value) tf.value=newvalue;
      }
      function trimstr(str,m){
        var count=0,ncode,ret="";
        for(var i=0;i<str.length;i++){
      ncode=(str.charCodeAt(i)>255)?2:1;
          if(count+ncode<=m){ret+=str.charAt(i);count+=ncode;
      }else{return ret
      }
    }
    return ret;
      }
    </script>
    <input id="tt" value="cvb" onpropertychange="trimtext(this,8);">
      

  6.   

    用文本框的onblur事件,调用一判断函数
    <script>
    function EnableLength(obj,len)
    {
      if(obj.value.length>len)
      {alert("字符长度超出允许长度,请重新输入");obj.focus();obj.select();}
    }
    </script>
    <html><input type=text name=test onblur="EnableLength(this,10)">
    </html>