Limit number of character entered into a textarea with javascript. Are you having troubles with users entering in crazy long responses to questions in your forms?, below you will find an example of how to limit the number of characters they can enter so the responses will fit nicely into the columns in your database. ( You may enter up to 125 characters. ) characters left textCounter() parameters are: text field, the count field, max length Code:
<SCRIPT LANGUAGE="JavaScript">
<!--
function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else
countfield.value = maxlimit - field.value.length;
}
// -->
</script>textCounter() parameters are: text field, the count field, max length
<form name=myform action="http://www.netevolution.co.uk">
<font size="1" face="arial, helvetica, sans-serif">
( You may enter up to 125 characters. )<br>
<textarea name=message wrap=physical cols=28 rows=4 onKeyDown="textCounter(this.form.message,this.form.remLen,125);" onKeyUp="textCounter(this.form.message,this.form.remLen,125);">
</textarea>
<br>
<input readonly type=text name=remLen size=3 maxlength=3 value="125">
characters left</font>
</form>