Sync files across computers for FREE click here for your free account



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

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




No mentions on Twitter yet. (Be the first to Tweet this post)

Host your site with EUKHost and Save 10%, use coupon 10%NEW, Dedicated, VPS, Shared, Linux, Windows, Reseller - save money on web hosting with our web hosting coupons.

Similar articles on NetEvolution.co.uk