Limit a textarea to a maximum lengthTag(s): Form
First technique, you validate on the submit the textarea length and return false if too long.
<SCRIPT LANGUAGE="JAVASCRIPT">
function checkLength(form){
if (form.description.value.length > 20){
alert("Text too long. Must be 20 characters or less");
return false;
}
return true;
}
</SCRIPT>
<FORM ACTION="..." METHOD="..." onSubmit="return checkLength(this)">
Second technique, as you typed, the length is checked. If the input is too long then it is truncated.
<script>
function limitText(limitField, limitNum) {
if (limitField.value.length > limitNum) {
limitField.value = limitField.value.substring(0, limitNum);
}
}
</script>
<form name="myform">
textarea limit 20 chars :<br>
<textarea rows="5" cols="30" onKeyDown="limitText(this,20);"
onKeyUp="limitText(this,20);">
</textarea><br>
</form>
mail_outline
Send comment, question or suggestion to howto@rgagnon.com
Send comment, question or suggestion to howto@rgagnon.com