How to add a text counter in textarea or Setting a maxlength on a textarea?

if you want to add text counter your textarea and set the Setting a maxlength on a textarea.

so simply add this code in file and run the file in your browser

<form name="textcount" id="textcount">
<textarea cols="60"  rows="12" class="input_compose_textarea" id="description" name="description" onkeypress="textCounter(this,this.form.counter,500);" onblur="textCounter(this,this.form.counter,500);"></textarea>
<br/>
Max length - <input    type="text" readonly="readonly"    name="counter"    maxlength="3"    size="3"    value="500"    onblur="textCounter(this.form.counter,this,500);"> char remaining.
</form>
<script type="application/javascript">
function textCounter( field, countfield, maxlimit ) {
if ( field.value.length > maxlimit )
{
field.value = field.value.substring( 0, maxlimit );
alert( 'Textarea value can only be 500 characters in length.' );
return false;
}
else
{
countfield.value = maxlimit - field.value.length;
}
}
</script>

Comments