Set Maxlength to TextArea
- Home
- Javascript
- Set Maxlength to TextArea
Question: I want to set maximum length to the textarea. I am using following code for the same,but it is not working,
<textarea name="txtDescription" cols=10 rows=3 maxlength=50></textarea>
But it is not working,it takes characters beyond 50.
This solution is re-usable for all text areas via one function and
it doesn't inform the user that he/she is typing too many characters, it prevents them from doing so, sort of like maxlength
The Function:
<script language="javascript" type="text/javascript">
<!--
function imposeMaxLength(Object, MaxLen)
{
return (Object.value.length <= MaxLen);
}
-->
</script>
Implementation:
<textarea name="myName" onkeypress="return imposeMaxLength(this, 15);" ><textarea>
HTML forms TextArea maxLength restrict entry