验证文本框的长度
<html xmlns="http://www.w3.org/1999/xhtml">?
<head>?
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />?
<title>无标题文档 </title>?
<script type="text/javascript">?
//计算长度?
function getLength(str)?
{?
var i=0;?
for(var j=0;j <str.length;j++)?
{?
if(str.charCodeAt(j) <0 || str.charCodeAt(j)>255)?
{?
? i+=2;?
}?
else?
{?
i++;?
}?
}?
return i;?
}?
</script>?
<SCRIPT language="javascript">?
function TxtMaxlength(MaxCount){?
var Objs=document.getElementById("TRecontent"); //取文本域对象?
var ObjCou=getLength(Objs.value); //取文本域长度?
//var ObjCou=Objs.value.length;?
? form1.YesCou.value=ObjCou+"/"+MaxCount;?
??
? if (ObjCou > MaxCount)?
? {?
? //操作指定大小把值重新赋值?
? var dd = Objs.value;
? for(var i=0;i<MaxCount;i++){
? Objs.value=dd.substr(0, MaxCount-i);?
? var ObjCou=getLength(Objs.value);
if(ObjCou<=20){
form1.YesCou.value=ObjCou+"/"+MaxCount;?
break;
}
? }
? }?
}?
</SCRIPT>?
</head>?
<body>?
<FORM method="POST" name="form1">?
<input name=YesCou size=4 value=0 readonly> <BR>?
<TEXTAREA name="TRecontent" id="TRecontent" rows="5" cols="50" onkeyup="TxtMaxlength(20)"> </TEXTAREA>?
</FORM>?
</body>?
</html>