Solved

Cross Browser Word Count for multiple text boxes

Posted on 2008-10-08
3
252 Views
Last Modified: 2013-11-07
We have some javascript code that we seem not to understand why it will work in IE and not FireFox.  This counts the words in several text boxes and disables the submit key if the count goes above the max.  This is working fine in IE, but does not work at all in any other browser.  Our first thought was that the JavaScript was not correctly identfiying the document, however we are unable to adjust the code correctly.

I have attached the JavaScript and one of the text boxes that currently resides in the Form.aspx page.  From the behind code page we are adding the onKeyup and onKeyDown to attributes of the text boxes. This way we can change which text boxes are included in the count and also the Max words based on business conditions.

Need some assistance in correcting JavaScript code to count words for the several text boxes that is true cross browser.
Form.Aspx

function disableSubmit(txtBody, btnSubmit,allowLengh,txtCurrent) 

{

   var txtBodyAll = new Array();

   

   txtBodyAll = txtBody.split(",")

   var tot = 0

  

   var btnCtl = document.getElementById(btnSubmit);

   var txtCurrentCount = document.getElementById(txtCurrent);

    

  

   for (var i =0; i < txtBodyAll.length; i++)

   {

       var txtBodyCtl = document.getElementById(txtBodyAll[i])

        var formcontent = txtBodyCtl.value;

        var formcontentArr = formcontent.split(" ");

        if (formcontentArr.length ==1)

        {

            if (txtBodyCtl.value!="" )

            {

                tot += 1

            }

            else

            {

            }

        }

        else

        {

            tot += formcontentArr.length 

        }

   }

    txtCurrentCount.value =tot

    

    if( parseInt (txtCurrentCount.value) > parseInt (allowLengh))    

        btnCtl.disabled = true;

    else

        btnCtl.disabled = false;    

}
 

<asp:TextBox id="txtLearningObjectives" runat="server" Width="66%" Height="51px" TextMode="MultiLine" ></asp:TextBox>
 

Form.Aspx.vb
 
 

txtLearningObjectives.Attributes.Add("onkeyup", "disableSubmit('" & sUniqueIDsForWordCount & "','" & cmdSubmit.UniqueID & "','" & maxLength & "','" & txtCurrentCount.UniqueID & "');")

txtLearningObjectives.Attributes.Add("onKeyDown", "disableSubmit('" & sUniqueIDsForWordCount & "','" & cmdSubmit.UniqueID & "','" & maxLength & "','" & txtCurrentCount.UniqueID & "');")

Open in new window

0
Comment
Question by:globalhealthcouncil
  • 2
3 Comments
 
LVL 7

Expert Comment

by:ASPSQLServerCOM
ID: 22673876
here try this code only for one text box in different browser and if it works then add another loop to go through each text box

Form.Aspx
function disableSubmit(txtBody, btnSubmit,allowLengh,txtCurrent)
{
   var txtBodyAll = new Array();
   
   txtBodyAll = txtBody.split(",")
   var tot = 0
 
   var btnCtl = document.getElementById(btnSubmit);
   var txtCurrentCount = document.getElementById(txtCurrent);
   
  var wordCount;
   var formcontent=document.getElementById(txtLearningObjectives).value
formcontent=formcontent.split(" ")
wordCount=formcontent.length;

   
    if( parseInt (wordCount) > parseInt (allowLengh))    
        btnCtl.disabled = true;
    else
        btnCtl.disabled = false;    
}
 
<asp:TextBox id="txtLearningObjectives" runat="server" Width="66%" Height="51px" TextMode="MultiLine" ></asp:TextBox>
 
Form.Aspx.vb
 
 
txtLearningObjectives.Attributes.Add("onkeyup", "disableSubmit('" & sUniqueIDsForWordCount & "','" & cmdSubmit.UniqueID & "','" & maxLength & "','" & txtCurrentCount.UniqueID & "');")
txtLearningObjectives.Attributes.Add("onKeyDown", "disableSubmit('" & sUniqueIDsForWordCount & "','" & cmdSubmit.UniqueID & "','" & maxLength & "','" & txtCurrentCount.UniqueID & "');")
0
 

Author Comment

by:globalhealthcouncil
ID: 22673897
this would then hard code the text boxes and these will vary based on other conditions.  For instance, if I use format 1 then it will be text boxes; a,b,c.  If I use format 2 it will be text boxes; a, c, d.  See how I need to keep the flexability for the differing conditions.

Yes, if I do as you suggest it works, but not as I need.
0
 
LVL 7

Accepted Solution

by:
ASPSQLServerCOM earned 500 total points
ID: 22698376
http://www.webcredible.co.uk/user-friendly-resources/dom-scripting/validate-forms-javascript.shtml
function validateForm(currentForm)

{
 

var blnvalidate = true;

 

var elementsInputs;
 
 

elementsInputs = currentForm.getElementsByTagName("input");

 

for (var intCounter = 0; intCounter < elementsInputs.length; intCounter++)

 

{

 

}

 }

Open in new window

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now