1.一个英文字母符号等代表一个字符,汉子代表两个字符
代码如下限制为200个字符:
<!DOCTYPE html>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head runat="server">
<title></title>
<script type="text/javascript">
var maxstrlen = 100;
function Q(s) {
return document.getElementById(s);
}
function checkWord(c) {
var str = c.value;
myLen = getStrleng(str);
var wck = Q("wordCheck");
if (myLen > maxstrlen * 2) {
c.value = str.substring(0, i + 1);
}
else {
wck.innerHTML = Math.floor((maxstrlen * 2 - myLen)).toString()+"/200";
}
}
function getStrleng(str) {
myLen = 0;
for (var i=0; (i < str.length) && (myLen <= maxstrlen * 2); i++) {
if (str.charCodeAt(i) > 0 && str.charCodeAt(i) < 128)
myLen++;
else
myLen += 2;
}
return myLen;
}
</script>
</head>
<body>
<div>
<input onkeyup="javascript:checkWord(this);" onmousedown="javascript:checkWord(this);"
name="content" style="overflow-y: scroll"/>
<span style="font-size: 26px;" id="wordCheck">200/200</span>
</div>
</body>
</html>