87,903
社区成员
发帖
与我相关
我的任务
分享
<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()
if(len>600)
alert("超过限制");
}
String.prototype.len = function() {
return this.replace(/[^\x00-\xff]/g, 'xx').length;
};
</script>
</head>
<body>
<div><input type=text id="test" /><input type=button value="测试" onclick=test()></div>
</body>
</html>
String.prototype.len = function() {
return this.replace(/[^\x00-\xff]/g, 'xx').length;
};
alert("文字abc".len());
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>判断输入的字数</title>
<mce:script type="text/javascript"><!--
function Checkval()
{
var s=document.getElementById("Text1").value;
var n=0;
for(var i=0;i<s.length;i++)
{
//charCodeAt()可以返回指定位置的unicode编码,这个返回值是0-65535之间的整数
if(s.charCodeAt(i)<128)
{
n++;
}
else
{
n+=2;
}
}
alert(n);
}
// --></mce:script>
</head>
<body>
<input id="Text1" type="text" onblur="Checkval()" />
</body>
</html>
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ws_hgo/archive/2009/07/31/4398515.aspx