Click to See Complete Forum and Search --> : Text area Validation Problem!!!
tryston02
09-16-2003, 04:11 PM
Hey guys, I've tried to write a javascript that checks to make sure that the user doesn't enter more than 300 words in a <text area></text> tag, and it would display an alert() notifying them......nothing I do seems to work. I can do it for any other text field using the length property, however, when it comes to the <text area> tag, it doesn't work. Could someone show me some code as to how to accomplish this? Thanks guys,
Khalid Ali
09-16-2003, 04:33 PM
text area itself does not know about the contents,so you will have to write a js function to parse the textarea contents and count the words in it.
requestcode
09-16-2003, 04:56 PM
Here is an excellent example that I found on another forum:
<script type="text/javascript">
<!--
function test(fldObj)
{
if (fldObj.value.length > 10)
fldObj.value = fldObj.value.substring (0, 10);
}
//-->
</script>
</head>
<body>
<textarea onchange="test(this)" onblur="test(this)"></textarea>
</body>
<textarea onchange = "if(this.value.length > 10) this.value = this.value.substring (0, 10);"
onblur="if(this.value.length > 10) this.value = this.value.substring (0, 10);"></textarea>
Shows a couple of different ways you can do it.