<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>限制多行文本域输入的字符个数</title>
<SCRIPT language=JavaScript>
<!--
var LastCount =0;
function CountStrByte(Message,Total,Used,Remain){ //字节统计
var ByteCount = 0;
var StrValue = Message.value;
var StrLength = Message.value.length;
var MaxValue = Total.value;
if(LastCount != StrLength) { // 在此判定,减少循环次数
for (i=0;i<StrLength;i++){
ByteCount = (StrValue.charCodeAt(i)<=256) ? ByteCount + 1 : ByteCount + 2;
if (ByteCount>MaxValue) {
Message.value = StrValue.substring(0,i);
alert("评论内容最多不能超过 " +MaxValue+ " 个字节!\n留意:一个汉字为两字节。");
ByteCount = MaxValue;
break;
}
}
Used.value = ByteCount;
Remain.value = MaxValue - ByteCount;
LastCount = StrLength;
}
}
//-->
</SCRIPT>
</head>
<body style="font-size:12px">
<table border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td> </td>
<td align="center" valign="middle"><table width="95%" border="0" cellspacing="0" cellpadding="0">
<form name="form1">
<tr>
<td width="23%" height="22" align="right" class="font2">评论人昵称:</td>
<td width="77%" height="22" align="left"><input name="评论人昵称" type="text" class="textbox" id="评论人昵称" /></td>
</tr>
<tr>
<td height="22" align="right" class="font2">评论内容:</td>
<td height="22" align="left"><textarea name="评论内容" cols="45" rows="5" id="评论内容" onKeyDown="CountStrByte(this.form.评论内容,this.form.total,this.form.used,this.form.remain);" onKeyUp="CountStrByte(this.form.评论内容,this.form.total,this.form.used,this.form.remain);"></textarea>
<br />
最多答应
<input name="total" type="text" disabled class="textbox" id="total" value="100" size="3">
个字节 已用字节:
<input name="used" type="text" disabled class="textbox" id="used" value="0" size="3">
剩余字节:
<input name="remain" type="text" disabled class="textbox" id="remain" value="100" size="3">
</td>
</tr>
<tr>
<td height="22"> </td>
<td height="22" align="left"><input name="add" type="submit" class="button" id="add" value="提 交" />
<input name="Submit2" type="reset" class="button" value="重 置" />
</td>
</tr>
</form>
</table></td>
</tr>
<tr>
<td height="100"> </td>
<td height="100"> </td>
</tr>
</table>
</body>
</html>