求一段代码
 判断文本框中的值中数字有多少 汉字有多少..然后相加
禁止使用for 谢谢咯 非常感谢 感谢咯.....

解决方案 »

  1.   

    a="不1会2写3作a业b就c来d问e下f我4!";
    nlength = a.length -a.replace( /\d/g, '').length;
    clength = ((c=escape(a.replace(/%u/g,''))).length-c.replace(/%u/g,'').length)/2;document.write( nlength+' '+clength);输出: 4 11说明呀,我没有完全解决你的问题的!
    clength 会是包含 中文字,全角英数符号,所有unicode的字符!
    还没有处理unicode字符中的只是汉字部分!不能满足的话,将要调用windows输入法类库,也只能在windows下的浏览器执行的了!
      

  2.   

    length 就可以
    想得到汉字个数就这样<html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
    <title>标题页</title>
    </head>
    <body>
    <script language="JavaScript"> 
    function cal(str)

        re=/[\u4E00-\u9FA5]/g;  //测试中文字符的正则
        if(re.test(str))        //使用正则判断是否存在中文
        return str.match(re).length //返回中文的个数
        else 
        return 0 

    </script> 
    <input onblur="alert(cal(this.value))"></body> 
    </body>
    </html>
      

  3.   

                var a = "不1会2写3作a业b就c来d问e下f我4",
                 reg = /[[\u4e00-\u9fa5]/g,
                 bytes = a.match(reg).length + a.length;
                alert(bytes);
      

  4.   

    document.getElementById('id').value().length...
      

  5.   

    js的  string的 length方法会认为汉字也是1个字符,所以你直接用.length就能拿到字符串的长度
      

  6.   

    <body>
    <input value='测试ab67' id='aa' type='text' />
    <input type='button' value='check' onclick='check()'/>
    <script>
        function check() {alert(document.getElementById('aa').value.length);}
    </script>
     </body>
      

  7.   

    你是想要字符数还是字节数?字符和字节都搞不清楚的。。
    想要字符数直接用length: var str = "测试ab67"; alert(str.length);
    想要字节数就看6楼
      

  8.   

    就是汉字占两个字节.然后数字字母占一个字节
    var a = "不1会2写3作a业b就c来d问e下f我4",
    reg = /[[\u4e00-\u9fa5]/g,
    bytes = a.match(reg).length + a.length;
    alert(bytes);
    这段代码算对总数了呀,傻瓜! 你一定要分开两个变量就改改嘛!
    var a = "不1会2写3作a业b就c来d问e下f我4",
    singlebytes = a.match( /[[\u0000-\u00ff]/g, '').length;
    doublebytes = a.length-singlebytes;alert('双字节的字节数:'+(doublebytes*2)+' 单字节:'+singlebytes);