不考虑中文,中文当作一个字符来处理。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script type="text/javascript">
//验证内容字数
function keyUp(){
var brief = document.form1.brief;
if ( 9 >= brief.value.length >= 1 ){
var nn = "0" + brief.value.length;
}else{
var nn = brief.value.length;
}
var num = document.getElementById('num');
var numsh = document.getElementById('numShYu');
if (parseInt(brief.value.length) >= 200){
alert("内容已达到最大字数,不能再输入!");
brief.value = brief.value.substring(0,200);
num.innerHTML = "200";
numsh.innerHTML = "0";
return false;
}
num.innerHTML = nn;
numsh.innerHTML = 200 - parseInt(brief.value.length);
}
</script>
</HEAD>
<BODY>
<form name=form1>
<div align="left">
<textarea onkeyup="keyUp();" name="brief" cols="90" rows="5" id="brief" style="width:366px;" ></textarea>
<font size=2>
(当前
<font color="#FF0000">
<span id='num'>0</span>
</font>
字,剩余
<font color="#FF0000">
<span id='numShYu'>200</span>
</font>
字)
</font>
</div>
</form>
</BODY>
</HTML>