<script language=javascript>
function gbcount(message,total,used,remain)
{
var max;
max = total.value;
if (message.value.length > max) {
message.value = message.value.substring(0,max);
used.value = max;
remain.value = 0;
alert("内容不允许超过 1000 个字!");
}
else {
var iLength = 0;
for(var i = 0;i<message.value.length;i++)
{
if(message.value.charCodeAt(i) >255)//字母数字的ascii编码都小于255而汉字的编码肯定大于255
{
iLength += 2;
}else if(message.value.charCodeAt(i)==32)//空格
{
iLength += 0;
}else//字母或数字
{
iLength += 1;
}
}
used.value=iLength;
//used.value = message.value.length;
remain.value = max - used.value;
}
}
</script>
<form>
<textarea onkeydown=gbcount(this.form.contents,this.form.total,this.form.used,this.form.remain); onkeyup=gbcount(this.form.contents,this.form.total,this.form.used,this.form.remain); cols="80" rows="8" wrap="VIRTUAL" id="contents">
</textarea>
最多字数:<INPUT disabled maxLength=4 name=total size=3 value=1000>
已用字数:<INPUT disabled maxLength=4 name=used size=3 value=0>
可用字数:<INPUT disabled maxLength=4 name=remain size=3 value=1000>
</form>
<htmel><title>字数</title>
<script language="javascript">
function gbcount(message,total,used,remain)
{
var max;
max=total.value;
if(message.value.length > max){
message.value = message.value.substring(0,max);
used.value = max;
remain.value = 0;
alert('不能超过300个字!');
}
else{
used.value = message.value.length;
remain.value = max - used.value;
}
}
</script>
<body><form>
<textarea cols="60" rows="8" id="memo1" onkeydown="gbcount(this.form.memo1,this.form.total1,this.form.used1,this.form.remain1);" onkeyup="gbcount(this.form.memo1,this.form.total1,this.form.used1,this.form.remain1);"></textarea>
<br>
最多字数:<INPUT disabled maxLength=4 name=total1 size=3 value=500>
已用字数:<INPUT disabled maxLength=4 name=used1 size=3 value=0>
剩余字数:<INPUT disabled maxLength=4 name=remain1 size=3 value=500> </form>
</body></html>
posted on 2007-09-17 13:09
larryjava 阅读(832)
评论(0) 编辑 收藏