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