function limitMessageLength(txtField,msgField,maxLength) { /* This routine is fired each time a character is typed into the text message box. It will display the number of characters remaining in a label. Calling program Usage: limitMessageLength("_l45","_l84","60"); Call it once in the FORM_OnInit() function. txtField: This is the id of the textArea field where the message is entered. msgField: This is the if of the field where you display the "characters remaining" message. maxLength: This is the maximum number of characters that can be entered in the txtField.
*/
var el = document.getElementById(txtField); // Get an instance of the message text area field. var charsUsed = document.getElementById(msgField); // Get an instance of the characters used field. el.onkeyup = function() { var textLength = el.value.length; // See how many characters have been typed in the field. var charsRemaining = maxLength - textLength; if (charsRemaining < 0) { el.value = el.value.substring(0,maxLength); charsUsed.style.color = "red"; charsUsed.innerText = "Maximum length reached."; } else { charsUsed.style.color = "#666666"; // Dark Gray color. charsUsed.innerText = charsRemaining + " characters remaining."; } }; } The above javascript sets up a keystroke watcher for the txtField you pass into it. Step 2: Now, in your FORM_OnInit() method, add the following call to the keystroke watcher. /* The javascript for this next function is located in limitMessageLength.js. It will limit the number of characters entered in the text area to 60. Once the user starts typing the textarea, a label field below the textarea will indicate the number of characters remaining.
"_l45" is the message text area field. "_l84" is the characters used field. "60" is the maximum characters that can be entered. */ limitMessageLength("_l45","_l84","60"); // Set up the keystroke watcher for the message text box. That's all there is to it. Once the user starts typing in the text area the "xx characters remaining" message will display and count down to 0 as the user types. You can add keystroke watchers for multiple fields in the same form. Just add one limitMessageLength() call for each field you want to limit in the FORM_OnInit() method. Hope this helps!
function FORM_OnInit() { limitMessageLength("_l164", "_l186", "60"); }
Alex,
Your syntax looks correct. The only thing that I can think of is that you may not be referencing the correct field number. For example, here is my call in the Form_OnInit() function:
limitMessageLength("_l45","_l84","850","_l89");
And here is the first field I am referencing in the first parameter (the textarea). (You can see the fields when you set your view to source):
fld border="1" col="8" color="" font="font-family:arial;font-style:normal;font-size:8pt;font-weight:normal;" height="2" id="textarea1" nbr="_l45" nm="" overflow="auto" par="TF0-0" readonly="false" row="1" tp="textArea" width="20" wrap="on"/
I notice your first parameter is "_l164". Are you sure you don't have an extra "1" in there? Also, make sure that is a lower case "L" right after the underscore rather than a number 1.
Thanks,
Randy