六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 22|回复: 0

js校验输入字节数

[复制链接]

升级  60%

6

主题

6

主题

6

主题

童生

Rank: 1

积分
30
 楼主| 发表于 2013-2-4 19:56:26 | 显示全部楼层 |阅读模式
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=gbk">
  <meta name="generator" content="PSPad editor, www.pspad.com">
  <title></title>
  </head>
  <body>
  <input type=text value="东岳大街123号C座"  id="test" name="test"><button >测试</button>
  </body>
  <script type="text/javascript">
    function getBytesLength(str) {
  // 在GBK编码里,除了ASCII字符,其它都占两个字符宽
      return str.replace(/[^\x00-\xff]/g, 'xx').length;
   }
   function showLen(){
        var word = document.getElementById("test").value;
        alert(getBytesLength(word));
    }
  </script>
</html>
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表