Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 284
  • Last Modified:

Cross Browser Word Count for multiple text boxes

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
globalhealthcouncil
Asked:
globalhealthcouncil
  • 2
1 Solution
 
ASPSQLServerCOMCommented:
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
 
globalhealthcouncilAuthor Commented:
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
 
ASPSQLServerCOMCommented:
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now