Hi I am using this in one of my pages. I need to add another to the same page, but some of the names need changed so the same script can be used in the same page twice. Just changing the function name didn't work. Can someone please help me alter this so I can use it again in the same page,.... so it doesn't conflict with this first one? Or tell me what I need to change? Thank you very much. <script type="text/javascript">function CheckSize(form, field) { if (field ==1) { Ctrl = form.comments; y = 300+1; } else { Ctrl = form.longdescription; y=300; } x = Ctrl.value.length; if (x < y) SendMsg (Ctrl, "Number of characters is: " + x); else SendMsg (Ctrl, "Warning! Description Too Long. Number of characters is: " + x);} function SendMsg (Ctrl, PromptStr) { alert (PromptStr) ; Ctrl.focus(); return;} </script> <textarea rows="2" cols="15" name="comments"></textarea><input value="Count Characters" onclick="CheckSize(this.form,1)" type="button" />
I think this is what you are after. <script type="text/javascript"> <!-- function CheckSize(form, field) { if (field ==1) { Ctrl = form.comments; y = 300+1; } else { Ctrl = form.longdescription; y=300; } x = Ctrl.value.length; if (x < y) SendMsg (Ctrl, "Number of characters is: " + x); else SendMsg (Ctrl, "Warning! Description Too Long. Number of characters is: " + x);} function SendMsg (Ctrl, PromptStr) { alert (PromptStr) ; Ctrl.focus(); return;} --> </script> <form name="the1stform"> <textarea rows="2" cols="15" name="comments"></textarea> <input value="Count Characters" onclick="CheckSize(this.form,1)" type="button" /> <br /> <textarea rows="2" cols="15" name="longdescription"></textarea> <input value="Count Characters" onclick="CheckSize(this.form,2)" type="button" /> </form> Hope this helps
seamus or someone else to help, you still around? The first one is picking up the character limit of the second. For instance the first one I have set at 200 characters and the second 300. The first one is only showing the warning when over 300 even though I kept the different names you gave me. Any way to adjust this "Warning! Description Too Long...." to accomodate each of their limits? Please let me know, thanks a bunch.
Hey Jen - This is the updated script. It is configured so that you can add it a multiplicity of times (unlimited actually). Ok here goes: <script type="text/javascript"> <!-- function SendMsg (Ctrl, PromptStr) { alert (PromptStr); Ctrl.focus(); return; } function CheckSize(form, field) { // 1 2 3 etc... var fieldNames = new Array("comments","longdescription", "anotherFieldName", "anotherFieldNameAgain", "youGetThePicture"); var fieldLimits = new Array(301, 300, 300, 300, 200); var fieldIndex = field - 1; if (fieldIndex < 0) { fieldIndex = 0;} if (fieldIndex > (fieldNames.length - 1)) { fieldIndex = fieldNames.length - 1;} Ctrl = form(fieldNames[fieldIndex]); y = fieldLimits[fieldIndex]; x = Ctrl.value.length; if (x <= y) { SendMsg (Ctrl, "Number of characters is: " + x); } else { SendMsg (Ctrl, "Warning! " + fieldNames[fieldIndex] + " Too Long. Number of characters is: " + x + " limit is " + y); } return true; } --> </script> <form name="the1stform"> <textarea rows="2" cols="15" name="comments"></textarea> <input value="Count Characters" onclick="CheckSize(this.form,1)" type="button" /> <br /> <textarea rows="2" cols="15" name="longdescription"></textarea> <input value="Count Characters" onclick="CheckSize(this.form,2)" type="button" /> </form>
Datatropics I love, it its like the perfect counter. Thank to you both so much. But can't get it working in Mozilla or opera. If you could help me get it working in those two only as well I'd really appreciate it. Thanks for taking the time to write it!! Jen
Ok here is the updated one: <script type="text/javascript"> <!-- function SendMsg (Ctrl, PromptStr) { alert (PromptStr); Ctrl.focus(); return; } function CheckSize(field) { // 1 2 3 etc... var fieldNames = new Array("comments","longdescription", "anotherFieldName", "anotherFieldNameAgain", "youGetThePicture"); var fieldLimits = new Array(301, 300, 300, 300, 200); var fieldIndex = field - 1; if (fieldIndex < 0) { fieldIndex = 0;} if (fieldIndex > (fieldNames.length - 1)) { fieldIndex = fieldNames.length - 1;} //Ctrl = form(fieldNames[fieldIndex]); Ctrl = document.getElementById(fieldNames[fieldIndex]); y = fieldLimits[fieldIndex]; x = Ctrl.value.length; if (x <= y) { SendMsg (Ctrl, "Number of characters is: " + x); } else { SendMsg (Ctrl, "Warning! " + fieldNames[fieldIndex] + " Too Long. Number of characters is: " + x + " limit is " + y); } return true; } --> </script> <form name="the1stform"> <textarea rows="2" cols="15" id="comments" name="comments"></textarea> <input value="Count Characters" onclick="CheckSize(1)" type="button" /> <br /> <textarea rows="2" cols="15" id="longdescription" name="longdescription"></textarea> <input value="Count Characters" onclick="CheckSize(2)" type="button" /> </form> You will notice that I chagned the function arguments, the assignment of Ctrl and then implemented an id specification for the form variables. Hope this works for ya!