Character Count in the Comments Section

Information on ASP, ASP.NET, PHP, HTML, Javascript and anything else WWW related.

Character Count in the Comments Section

Postby admin on Wed Oct 29, 2008 10:06 pm

So I had a customer that wanted to let his site users to know how many characters they had typed into a comments field as he was trying to limit the comments to 300 characters or less. Here's how I accomplished it.

First, add the javascript to the head of the page:
Code: Select all
<script language="javascript" type="text/javascript">
<!--       
            function CharacterCount(sourceID, countID, maxLength)
            {
                        var source = document.getElementById(sourceID);
                        var count = document.getElementById(countID);
                        if(source.value.length > maxLength)
                                    source.value = source.value.substr(0,maxLength);

                        count.value = source.value.length;
            }
//-->
</script>

and basically, form1 is the name of the form on the page and CharacterCount is the name of the field doing the counting. The comments is the name of the textarea in the form1 form. This script also only allows the comments field to have 300 characters typed into it.

then I added the following to the page:
Code: Select all
<textarea name="comments" cols="40" rows="5" id="comments" onKeyUp="CharacterCount(this.id, 'CommentLength', 160)"></textarea>

That is the actual comments textarea...
Code: Select all
Character Count <input name="CommentLength" type="text" class="txtboxes" id="CommentLength" size="5" maxlength="3"  readonly="true"/>

The above is the actual field that counts the characters. You need to set it to readonly so that the user doesn't try to put something in that field.

You can see this in action here: http://www.forstco.ws/support/contact_us.asp

*** John Glass gets the credit for helping me w/ this one ***
admin
Site Admin
 
Posts: 6
Joined: Wed Oct 29, 2008 12:44 pm

Return to Web Site Assistance

Who is online

Users browsing this forum: No registered users and 1 guest

cron