Solved

Cross Browser Word Count for multiple text boxes

Posted on 2008-10-08
3
255 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

863 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

22 Experts available now in Live!

Get 1:1 Help Now