<HTML>
<HEAD>
<TITLE>test</TITLE>
<meta http-equiv=content-type content="text/html; charset=UTF-8">
<script language=javascript>
function test(){
var testInput=document.getElementById("test").value;
var len=testInput.len() ;
alert(len);
if(len>600)
alert("超过限制");
}
String.prototype.len = function() {
//xxx表示一个字符占的长度(字节数).utf-8是3个
return this.replace(/[^\x00-\xff]/g, 'xxx').length;
};
</script>
<div>
<input type=text id="test" />
<input type=button value="测试" οnclick=test()>
</div>
</body>
</html>
<HEAD>
<TITLE>test</TITLE>
<meta http-equiv=content-type content="text/html; charset=UTF-8">
<script language=javascript>
function test(){
var testInput=document.getElementById("test").value;
var len=testInput.len() ;
alert(len);
if(len>600)
alert("超过限制");
}
String.prototype.len = function() {
//xxx表示一个字符占的长度(字节数).utf-8是3个
return this.replace(/[^\x00-\xff]/g, 'xxx').length;
};
</script>
</head>
// 在gbk编码里,除了ascii字符,其它都占两个字符宽
<div>
<input type=text id="test" />
<input type=button value="测试" οnclick=test()>
</div>
</body>
</html>