Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. IE7 Javascript Problem

IE7 Javascript Problem

Scheduled Pinned Locked Moved Web Development
helpjavascriptquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Infomine Liam
    wrote on last edited by
    #1

    I'm trying to do some form validation that involves looping through all elements in a form. It works perfectly in FireFox, but totally chokes in IE7. It seems to be that the $Form.elements.length value is not returning a number. I've written a little test function to simulate the error. function validateForm() { alert(document.forms.length); var numElements = document.forms[0].elements.length; alert(numElements); } First alert shows: 1 Second alert shows: [object] Anyone seen this happen before? Thanks a lot. :cool:

    T 1 Reply Last reply
    0
    • I Infomine Liam

      I'm trying to do some form validation that involves looping through all elements in a form. It works perfectly in FireFox, but totally chokes in IE7. It seems to be that the $Form.elements.length value is not returning a number. I've written a little test function to simulate the error. function validateForm() { alert(document.forms.length); var numElements = document.forms[0].elements.length; alert(numElements); } First alert shows: 1 Second alert shows: [object] Anyone seen this happen before? Thanks a lot. :cool:

      T Offline
      T Offline
      Torsten Mauz
      wrote on last edited by
      #2

      try something like this

      function checkForm()
      {
      var frm = document.getElementById('form1');
      var strError = 'The following fields are required:\n';
      var bValid = true;
      var child;

      for(var i = 0; i < frm.childNodes.length; i++)
      {
          child = frm.childNodes.item(i);
          if(child.nodeType == 1)
          {
              if(child.tagName.toString().toLowerCase() == 'input')
              {
                  if(child.type.toString().toLowerCase() == 'text')
                  {
                      if(child.value == '')
                      {
                          strError += '\\n'+ child.id.toString();
                          bValid = false;
                      }
                  }
              }
          }
      }
      if(!bValid)
          alert(strError);
      return bValid;
      

      }

      Works in both FF and IE7. It only checks input text's at the moment so you'll need to change it so that it's a bit more useful, but it should get you going. HTH

      I 1 Reply Last reply
      0
      • T Torsten Mauz

        try something like this

        function checkForm()
        {
        var frm = document.getElementById('form1');
        var strError = 'The following fields are required:\n';
        var bValid = true;
        var child;

        for(var i = 0; i < frm.childNodes.length; i++)
        {
            child = frm.childNodes.item(i);
            if(child.nodeType == 1)
            {
                if(child.tagName.toString().toLowerCase() == 'input')
                {
                    if(child.type.toString().toLowerCase() == 'text')
                    {
                        if(child.value == '')
                        {
                            strError += '\\n'+ child.id.toString();
                            bValid = false;
                        }
                    }
                }
            }
        }
        if(!bValid)
            alert(strError);
        return bValid;
        

        }

        Works in both FF and IE7. It only checks input text's at the moment so you'll need to change it so that it's a bit more useful, but it should get you going. HTH

        I Offline
        I Offline
        Infomine Liam
        wrote on last edited by
        #3

        Thanks very much for the reply, but I guess my main question here is why $Form.elements.length is not returning a number. This has always worked before....

        T 1 Reply Last reply
        0
        • I Infomine Liam

          Thanks very much for the reply, but I guess my main question here is why $Form.elements.length is not returning a number. This has always worked before....

          T Offline
          T Offline
          Torsten Mauz
          wrote on last edited by
          #4

          Wierd, just done a tiny test myself using your code and get a number as you'd expect from elements.length Here is the code I ran:

          function checkForm() { alert(document.forms.length); var numElements = document.forms[0].elements.length; alert(numElements); return true; } The first alert gives "1" and the second gives "5". If your form isn't too huge perhaps you could paste in the code that's giving you this wierd behaviour. HTH

          I 1 Reply Last reply
          0
          • T Torsten Mauz

            Wierd, just done a tiny test myself using your code and get a number as you'd expect from elements.length Here is the code I ran:

            function checkForm() { alert(document.forms.length); var numElements = document.forms[0].elements.length; alert(numElements); return true; } The first alert gives "1" and the second gives "5". If your form isn't too huge perhaps you could paste in the code that's giving you this wierd behaviour. HTH

            I Offline
            I Offline
            Infomine Liam
            wrote on last edited by
            #5

            Well, I feel dumb now. I had a textbox in the form named "length". :doh: Thanks for all your help.

            T 1 Reply Last reply
            0
            • I Infomine Liam

              Well, I feel dumb now. I had a textbox in the form named "length". :doh: Thanks for all your help.

              T Offline
              T Offline
              Torsten Mauz
              wrote on last edited by
              #6

              hehe - one of those ;)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups