Restricting the amount of data a user can enter in a textarea form field
If you want to limit how many characters a user enters in a Text Area form field, you can use the onKeyDown function to check the length of the string with the key the user just pressed. If it exceeds a certain value, you can actually cancel that keypress.
Example HTML/CFML code: Code:
<html>
<head>
<script language="Javascript1.2">
function checkLength()
{
if (document.MyForm.Desc.value.length > 9)
{
alert('You may only enter 10 characters in the description field.');
return false;
}
}
</script>
</head>
<body onLoad="document.MyForm.Desc.focus()">
<form name="MyForm">
Please tell us a <i>little</i> bit about yourself (10 characters max)<br>
<textarea name="Desc" rows="4" cols="40" wrap="off" onKeyDown="return checkLength(event,'1')"></textarea>
</form>
</body>