<script> 
function checkstr(str) 

num=str.length
var arr=str.match(/[^\x00-\x80]/ig)
if(arr!=null)num+=arr.length
alert(num)

</script>
<input id="msg"><input type=button onclick=checkstr(msg.value) value="检查">

解决方案 »

  1.   

    function strLen(sCounted)
    {
    var nLen=0, sSub, sCode, nI;
    sCounted=String(sCounted);
    for(nI=0; nI<sCounted.length;nI++)
    {
    sSub=sCounted.substring(nI, nI+1);
    sCode=escape(sSub);
    if(sCode.length>3)
    nLen+=2;
    else
    nLen+=1;
    }
    return nLen;
    }
      

  2.   

    http://www.csdn.net/Develop/read_article.asp?id=13104
      

  3.   

    给字符串定义一个方法
    /*** 返回字节数 ***/
    String.prototype.lenb = function() {
      return this.replace(/[^\x00-\xff]/g,"**").length;
    }
      

  4.   

    给你一个函数:
    function getStringLength(str) {
        if (str == null || str.length == 0) {
            return 0;
        }
        var strLong = 0;
        var browserLen  = str.length;
        var result      = 0;
        var charCode    = 0;
        for (i=0; i<browserLen; i++) {
            charCode = str.charCodeAt(i);
            if (charCode > 255) {
                strLong += 2;
            } else {
             strLong += 1;
            }
        }
        return strLong;
    }
      

  5.   

    <script language=javascript>
    function String.prototype.lenB(){return this.replace(/[^\x00-\xff]/g,"**").length;}
    var str = "这是一个可以将汉字计算成两个字节的函数";
    alert("老的算法 = " + str.length);
    alert("新的算法 = " + str.lenB());
    </script>
      

  6.   

    我写的
    //取得字符串的字节长度
    function ByteLen(Str)
    {
    var Len=0;
    for(var i=0;i<Str.length;i++){
    if(Str.charCodeAt(i)>255){
    Len+=2;
    }
    else Len+=1;
    }
    return(Len);
    }