View Full Version : Counting characters Text Area: replace two words, and it won't work

12-23-2010, 04:18 PM

I have this snippet for controlling the number of characters entered in a textarea. It works.

But, if I replace in this code the name "my_form" by "contact-form" and "my_text" by "message", it doesnt ! It looks like the keyword is "my_form". You change that , and the script will not work!

It turns out that I have to change those names because the form as it was already had those names my_form and message before I added this counter And I dont want to replace the names of the form in order to adapt them to this code snippet below.

The question is why would the script not work if I have just replaced two words, which dont seem to be keywords of JS programming (my_form and my_text)

<title>(Type a title for your page here)</title>

<script language=JavaScript>
function check_length(my_form)
maxLen = 50; // max number of characters allowed
if (my_form.my_text.value.length >= maxLen) {
// Alert message if maximum limit is reached.
// If required Alert can be removed.
var msg = "You have reached your maximum limit of characters allowed";
// Reached the Maximum length so trim the textarea
my_form.my_text.value = my_form.my_text.value.substring(0, maxLen);
else{ // Maximum length not reached so update the value of my_text counter
my_form.text_num.value = maxLen - my_form.my_text.value.length;}


<form name=my_form method=post>
<textarea onKeyPress=check_length(this.form); onKeyDown=check_length(this.form); name=my_text rows=4 cols=30></textarea>
<input size=1 value=50 name=text_num> Characters Left


12-23-2010, 06:32 PM
I found the answer. For whatever the reason, it seems that my_form is a JS keyword. Couldnt find it in the doc, but did the trick

Philip M
12-23-2010, 06:39 PM
my_form is not a js keyword.

The trouble is the illegal hyphen in contact-form. Change that to contact_form and it works fine.

<script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead.
The <!-- and //--> comment (hiding) tags have not been necessary since IE3 (i.e. since September 1997). If you see these in some published script it is a warning that you are looking at ancient and perhaps unreliable code.

"...there are indications that the severest phase of the recession is over..."
- Harvard Economic Society (HES) Jan 18, 1930

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum