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.
function checkLength(form){
    if (form.description.value.length > 20){
        alert("Text too long. Must be 20 characters or less");
        return false;
    return true;

<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.

function limitText(limitField, limitNum) {
    if (limitField.value.length > limitNum) {
        limitField.value = limitField.value.substring(0, limitNum);

<form name="myform">
textarea limit 20 chars :<br>
<textarea rows="5" cols="30" onKeyDown="limitText(this,20);" 

textarea limit 20 chars :

thanks to wsperry for the tip
blog comments powered by Disqus