当前位置: 代码迷 >> JavaScript >> JS怎么判断textarea的内容长度
  详细解决方案

JS怎么判断textarea的内容长度

热度:122   发布时间:2012-02-21 16:26:23.0
JS如何判断textarea的内容长度
JS如何判断textarea的内容长度,我用if   (document.add.a59.value.length <100){alert( "不能少于100字! ");add.a59.focus();return;}不能实现   多谢指点


------解决方案--------------------
你的这些写在了哪里?
试试
if (document.add.a59.value.length <100){alert( "不能少于100字! ");add.a59.focus();return false;}


------解决方案--------------------
<html>

<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> New Page 2 </title>
</head>

<body>
<textarea > </textarea> <input type=button value=check onclick= "checkMaxLength(this.previousSibling,20) "
</body>

</html>
<script>
/**
*checkMaxLength.
*JK 2006-11-26
*/
function checkMaxLength(textareaObj,maxLength)
{
if(maxLength == null) maxLength=textareaObj.getAttribute( "maxLength ");
if(maxLength == null) maxLength=1024;
var currentLength = textareaObj.value.length;
if (currentLength > maxLength) {
alert( "The length of your input ( "+currentLength+ ") is larger than the maxLength ( "+maxLength+ ") . ");
if(textareaObj.createTextRange){
var textRange=textareaObj.createTextRange();
textRange.moveStart( 'character ',10);
textRange.select();
}
else textareaObj.select();
textareaObj.focus();
return false;
}
return true;
}


</script>
  相关解决方案