Limiting Textarea Text
2. The Script
To start with, here's the code for our textarea field:
This passes the current text area field along with a character limit (in this case 40) to a function called textLimit. It is this function which creates the nice effect of having the maximum field size match the size of the field. If you use a different value for the second argument then that will be used as the maximum field size and your visitor will either be unable to fill the complete field (if you make it smaller) or the field will be scrollable (if you make it bigger).
if (field.value.length > maxlen + 1)
alert('your input has been truncated!');
if (field.value.length > maxlen)
field.value = field.value.substring(0, maxlen);
The two lines that are not in bold produce the message that is displayed when your visitor tries pasting something into the field and more than one character is lost when the field is truncated. This let's your visitor know that what they have tried to paste has made the content of the field much larger than is allowed and that the excess will be truncated. If you don't want a message in this instance then just omit these two lines.