View Full Version : Muli-Field Character Count Total

06-04-2004, 03:18 PM
The following script basically counts the characters in a text area and shows the number of characters on the fly. I need to have the combined total of four text areas limited to an amount and ideally would like it to display the count as this script does. For example: a contact form that would contain a "Name", "E-Mail", "Phone" and "Message" field that when combined can only reach an X number of characters. I apologize in advance - I am new.


<script language = "Javascript">
* DHTML textbox character counter (IE4+) script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)

function taLimit() {
var taObj=event.srcElement;
if (taObj.value.length==taObj.maxLength*1) return false;

function taCount(visCnt) {
var taObj=event.srcElement;
if (taObj.value.length>taObj.maxLength*1) taObj.value=taObj.value.substring(0,taObj.maxLength*1);
if (visCnt) visCnt.innerText=taObj.maxLength-taObj.value.length;

Willy Duitt
06-04-2004, 06:14 PM
FWIW: The script you posted is IE only.
Are you really not interested in cross-browser compatability?

But to answer your question. Set a global variable to increment with your taObj.value.length and use that value to reach your maximum character limit.

The question is. What happens if a user goes over the character limit? Which field will it effect? I would assume the message, but we would need more information. Particularly some code...

06-04-2004, 06:27 PM
Your response is greatly appreciated!

Forgive me for lack of information. I am definitely interested in cross browser compatibility, but I am new and don't know what I need to add to make that happen. It would be fine with the character limit showing up in the Message text area since it will be the final area that is filled out (Potentially flawed? Perhaps, but it would do for now). Unfortunately I also do not know how to set a global variable. Any additional help is appreciated. Here is the HTML code that goes along with the previous posted script:

<font> Maximum Number of characters for this text box is 255.<br>
<TEXTAREA onkeypress="return taLimit()" onkeyup="return taCount(myCounter)"
name=Description rows=7 wrap=physical cols=40 maxLength="255"></TEXTAREA>
You have <B><SPAN id=myCounter>255</SPAN></B> characters remaining
for your description...</font>

Willy Duitt
06-04-2004, 06:43 PM
I am not inclined to write a complete form just to demonstrate a few lines of code. If you post your entire form, I will look into writing a cross browser script.

06-04-2004, 07:55 PM
Thanks for you assistance, I'm sorry I didn't post more code. I didn't realize you wanted the whole form. I have attached the form with the counter script.

06-08-2004, 08:50 PM
Hi everybody.

If anyone else wants to give this a go, please feel free - it would be greatly appreciated.

06-09-2004, 05:10 PM
Here is a link to a form that basically does what I want it to (counts the total characters of specified fields and limits them to a certain amount - result is reflected with the running total)