<script>
var ns6=document.getElementById&&!document.allfunction restrictinput(maxlength,e,placeholder){
if (window.event&&event.srcElement.value.length>=maxlength)
return false
else if (e.target&&e.target==eval(placeholder)&&e.target.value.length>=maxlength){
var pressedkey=/[a-zA-Z0-9\.\,\/]/ //detect alphanumeric keys
if (pressedkey.test(String.fromCharCode(e.which)))
e.stopPropagation()
}
}function countlimit(maxlength,e,placeholder){
var theform=eval(placeholder)
var lengthleft=maxlength-theform.value.length
var placeholderobj=document.all? document.all[placeholder] : document.getElementById(placeholder)
if (window.event||e.target&&e.target==eval(placeholder)){
if (lengthleft<0)
theform.value=theform.value.substring(0,maxlength)
placeholderobj.innerHTML=lengthleft
}
}
function displaylimit(theform,thelimit){
var limit_text='<b><span id="'+theform.toString()+'">'+thelimit+'</span></b> characters remaining on your input limit'
if (document.all||ns6)
document.write(limit_text)
if (document.all){
eval(theform).onkeypress=function(){ return restrictinput(thelimit,event,theform)}
eval(theform).onkeyup=function(){ countlimit(thelimit,event,theform)}
}
else if (ns6){
document.body.addEventListener('keypress', function(event) { restrictinput(thelimit,event,theform) }, true); 
document.body.addEventListener('keyup', function(event) { countlimit(thelimit,event,theform) }, true); 
}
}</script></head><body><form name="sampleform"><input type="text" name="george" size=20><br>
<script>
displaylimit("document.sampleform.george",115)
</script>
<p>
<textarea name="john" cols=25 rows=15></textarea><br>
<script>
displaylimit("document.sampleform.john",110)
</script></form></body>
</html>

解决方案 »

  1.   

    <HTML>
    <HEAD>
    <SCRIPT language=JavaScript>
    function strlength(str){
        var l=str.length;
        var n=l
        for (var i=0;i<l;i++)
        {
            if (str.charCodeAt(i)<0||str.charCodeAt(i)>255) n++
        }
        return n        
    }
    function changebyte(value,length){
        var l=strlength(value)
        if (l<=length) {
            if (document.all!=null) document.all("byte").innerText="&raquo;&sup1;&iquest;&Eacute;&Ograve;&Ocirc;&Ecirc;&auml;&Egrave;&euml;"+(length-l)+"×&Ouml;&frac12;&Uacute;"
        }
        else
        {
    alert('&Ecirc;&auml;&Egrave;&euml;×&Ouml;&frac12;&Uacute;&Ecirc;&yacute;&sup3;&not;&sup3;&ouml;·&para;&Icirc;§');
            document.all("byte").innerText="&Ecirc;&auml;&Egrave;&euml;×&Ouml;&frac12;&Uacute;&Ecirc;&yacute;&sup3;&not;&sup3;&ouml;·&para;&Icirc;§"
    return false;
        }
        return true
    }
    </SCRIPT>
    </HEAD>
    <BODY>
    <FORM name=test onsubmit="return checkdata()" action="" method=post>
    <TEXTAREA onkeydown="return changebyte(document.test.icqcontent.value,20)" onkeyup="return changebyte(document.test.icqcontent.value,20)" name=icqcontent rows=3 cols=40>
    </TEXTAREA> 
    <SPAN id=byte style="font-size:12px">
    <SCRIPT language=JavaScript>
    changebyte1(document.test.icqcontent.value,20);
    </SCRIPT>
    </SPAN></FORM></BODY></HTML>
      

  2.   

    更正.......
    <HTML>
    <HEAD>
    <SCRIPT language=JavaScript>
    function strlength(str){
        var l=str.length;
        var n=l
        for (var i=0;i<l;i++)
        {
            if (str.charCodeAt(i)<0||str.charCodeAt(i)>255) n++
        }
        return n        
    }
    function changebyte(value,length){
        var l=strlength(value)
        if (l<=length) {
            if (document.all!=null) document.all("byte").innerText="还可以输入"+(length-l)+"字节"
        }
        else
        {
    alert('输入字节数超出范围');
            document.all("byte").innerText="输入字节数超出范围"
    return false;
        }
        return true
    }
    </SCRIPT>
    </HEAD>
    <BODY>
    <FORM name=test onsubmit="return checkdata()" action="" method=post>
    <TEXTAREA onkeydown="return changebyte(document.test.icqcontent.value,20)" onkeyup="return changebyte(document.test.icqcontent.value,20)" name=icqcontent rows=3 cols=40>
    </TEXTAREA> 
    <SPAN id=byte style="font-size:12px">
    <SCRIPT language=JavaScript>
    changebyte1(document.test.icqcontent.value,20);
    </SCRIPT>
    </SPAN></FORM></BODY></HTML>
      

  3.   

    longshenwang(独眼龙) :  但是达到长度后却无法编辑了啊
      

  4.   

    想要达到长度就无法编辑:
    if (l>=length) {//判断如果长度l超过给定值length  则封键盘
    window.event.cancelBuuble=true;
    window.event.returnValue=false;
    }
      

  5.   

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <!-- saved from url=(0046)http://www.blueidea.com/user/poorfish/form.htm -->
    <HTML><HEAD>
    <META http-equiv=Content-Type content="text/html; charset=unicode">
    <SCRIPT language=JavaScript>
    function strlength(str){
        var l=str.length;
        var n=l
        for (var i=0;i<l;i++)
        {
            if (str.charCodeAt(i)<0||str.charCodeAt(i)>255) n++
        }
        return n        
    }function changebyte(value,length){
        var l=strlength(value)
        if (l<=length) {
            if (document.all!=null) document.all("byte").innerText="还可以输入"+(length-l)+"字节"
        }
        else
        {
            document.all("byte").innerText="输入字节数超出范围"
        }
        return true
    }function changebyte1(value,length){
        var l=strlength(value)
        if (l<=length) {
            if (document.all!=null) document.all("byte1").innerText="还可以输入"+(length-l)+"字节"
        }
        else
        {
            document.all("byte1").innerText="输入字节数超出范围"
        }
        return true
    }function changebyte2(value,length){
        var l=strlength(value)
        if (l<=length) {
            if (document.all!=null) document.all("byte2").innerText="还可以输入"+(length-l)+"字节"
        }
        else
        {
            document.all("byte2").innerText="输入字节数超出范围"
        }
        return true
    }
    </SCRIPT><FORM name=test onsubmit="return checkdata()" action="" method=post>
    <META content="Microsoft FrontPage 5.0" name=GENERATOR></HEAD>
    <BODY><TEXTAREA onkeydown="return changebyte1(document.test.icqcontent.value,20)" onkeyup="return changebyte1(document.test.icqcontent.value,20)" name=icqcontent rows=3 cols=40></TEXTAREA> 
    <SPAN id=byte1 style="font-size:12px">
    <SCRIPT 
    language=JavaScript>changebyte1(document.test.icqcontent.value,20);</SCRIPT>
    </SPAN></FORM></BODY></HTML>
      

  6.   

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <!-- saved from url=(0046)http://www.blueidea.com/user/poorfish/form.htm -->
    <HTML><HEAD>
    <META http-equiv=Content-Type content="text/html; charset=unicode">
    <SCRIPT language=JavaScript>
    function strlength(str){
        var l=str.length;
        var n=l
        for (var i=0;i<l;i++)
        {
            if (str.charCodeAt(i)<0||str.charCodeAt(i)>255) n++
        }
        return n        
    }function changebyte(value,length){
        var l=strlength(value)
        if (l<=length) {
            if (document.all!=null) document.all("byte").innerText="&raquo;&sup1;&iquest;&Eacute;&Ograve;&Ocirc;&Ecirc;&auml;&Egrave;&euml;"+(length-l)+"×&Ouml;&frac12;&Uacute;"
        }
        else
        {
            document.all("byte").innerText="&Ecirc;&auml;&Egrave;&euml;×&Ouml;&frac12;&Uacute;&Ecirc;&yacute;&sup3;&not;&sup3;&ouml;·&para;&Icirc;§"
        }
        return true
    }function changebyte1(value,length){
        var l=strlength(value)
        if (l<=length) {
            if (document.all!=null) document.all("byte1").innerText="&raquo;&sup1;&iquest;&Eacute;&Ograve;&Ocirc;&Ecirc;&auml;&Egrave;&euml;"+(length-l)+"×&Ouml;&frac12;&Uacute;"
        }
        else
        {
            document.all("byte1").innerText="&Ecirc;&auml;&Egrave;&euml;×&Ouml;&frac12;&Uacute;&Ecirc;&yacute;&sup3;&not;&sup3;&ouml;·&para;&Icirc;§"
        }
        return true
    }function changebyte2(value,length){
        var l=strlength(value)
        if (l<=length) {
            if (document.all!=null) document.all("byte2").innerText="&raquo;&sup1;&iquest;&Eacute;&Ograve;&Ocirc;&Ecirc;&auml;&Egrave;&euml;"+(length-l)+"×&Ouml;&frac12;&Uacute;"
        }
        else
        {
            document.all("byte2").innerText="&Ecirc;&auml;&Egrave;&euml;×&Ouml;&frac12;&Uacute;&Ecirc;&yacute;&sup3;&not;&sup3;&ouml;·&para;&Icirc;§"
        }
        return true
    }
    </SCRIPT><FORM name=test onsubmit="return checkdata()" action="" method=post>
    <META content="MSHTML 6.00.2600.0" name=GENERATOR></HEAD>
    <BODY><TEXTAREA onkeydown="return changebyte1(document.test.icqcontent.value,20)" onkeyup="return changebyte1(document.test.icqcontent.value,20)" name=icqcontent rows=3 cols=40></TEXTAREA> 
    <SPAN id=byte1 style="font-size:12px">
    <SCRIPT 
    language=JavaScript>changebyte1(document.test.icqcontent.value,20);</SCRIPT>
    </SPAN></FORM></BODY></HTML>