js校验输入字节数

<!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 onclick="showLen();">测试</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>

你可能感兴趣的:(JavaScript,html,C++,c,C#)