JavaScript Validation Works In Firefox But Not IE

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SJ Carter

    JavaScript Validation Works In Firefox But Not IE

    Hello guys,

    I have the following piece of code:
    function validate()
    {
    var f = document.form1;
    if (f.name.value == "" || f.name.value == '' || f.name.value.le ngth
    == 0)
    {
    alert('Please enter your name');
    return false;
    }
    return true;
    }


    It works fine in Firefox, and when I use firebug to inspect the code I
    don't get any errors. However, it doesn't work for IE. Any reasons
    for that, and how can I fix this problem? Thanks.
  • Joost Diepenmaat

    #2
    Re: JavaScript Validation Works In Firefox But Not IE

    SJ Carter <sjmcarter@gmai l.comwrites:
    Hello guys,
    >
    I have the following piece of code:
    function validate()
    {
    var f = document.form1;
    if (f.name.value == "" || f.name.value == '' || f.name.value.le ngth
    == 0)
    {
    alert('Please enter your name');
    return false;
    }
    return true;
    }
    >
    >
    It works fine in Firefox, and when I use firebug to inspect the code I
    don't get any errors. However, it doesn't work for IE. Any reasons
    for that, and how can I fix this problem? Thanks.
    You probably really shouldn't have a form element named "name".

    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

    Comment

    • Joost Diepenmaat

      #3
      Re: JavaScript Validation Works In Firefox But Not IE

      Joost Diepenmaat <joost@zeekat.n lwrites:
      You probably really shouldn't have a form element named "name".
      or if you do, you can probably use form.elements.n ame.value instead of
      form.name.value

      --
      Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

      Comment

      • SJ Carter

        #4
        Re: JavaScript Validation Works In Firefox But Not IE

        On May 14, 3:30 pm, Joost Diepenmaat <jo...@zeekat.n lwrote:
        Joost Diepenmaat <jo...@zeekat.n lwrites:
        You probably really shouldn't have a form element named "name".
        >
        or if you do, you can probably use form.elements.n ame.value instead of
        form.name.value
        >
        --
        Joost Diepenmaat | blog:http://joost.zeekat.nl/| work:http://zeekat.nl/
        Thanks Joost, but it still isn't working. I tried changing the form
        element name with no success, and then tried using the
        "form.elements. ...", with identical results.

        Comment

        • Joost Diepenmaat

          #5
          Re: JavaScript Validation Works In Firefox But Not IE

          SJ Carter <sjmcarter@gmai l.comwrites:
          On May 14, 3:30 pm, Joost Diepenmaat <jo...@zeekat.n lwrote:
          >Joost Diepenmaat <jo...@zeekat.n lwrites:
          You probably really shouldn't have a form element named "name".
          >>
          >or if you do, you can probably use form.elements.n ame.value instead of
          >form.name.valu e
          >
          Thanks Joost, but it still isn't working. I tried changing the form
          element name with no success, and then tried using the
          "form.elements. ...", with identical results.
          it'd probably help if you post a link to the complete page.

          --
          Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: JavaScript Validation Works In Firefox But Not IE

            SJ Carter wrote:
            On May 14, 3:30 pm, Joost Diepenmaat <jo...@zeekat.n lwrote:
            >Joost Diepenmaat <jo...@zeekat.n lwrites:
            >>You probably really shouldn't have a form element named "name".
            >or if you do, you can probably use form.elements.n ame.value instead of
            >form.name.valu e
            >
            Thanks Joost, but it still isn't working.
            Post the error message, then.
            I tried changing the form element name with no success, and then tried
            using the "form.elements. ...", with identical results.
            DOM terms collide with markup terms here. Just to clarify, you should not
            have a form *control* named "name", i.e. you should not have

            <input name="name" ...>

            because the form *element*

            <form ... name="foo">
            ...
            </form>

            would then cause formRef.name to be evaluated to "foo" instead of the input
            object. Modifying or removing the `name' attribute value of the `form'
            element would not change that, which could explain what you observed.

            You have to change the value of the `name' attribute of the `input' element
            instead. BTW, "submit" is another name that should be avoided, as form
            objects have a submit() method.

            Please trim your quotes.


            PointedEars
            --
            realism: HTML 4.01 Strict
            evangelism: XHTML 1.0 Strict
            madness: XHTML 1.1 as application/xhtml+xml
            -- Bjoern Hoehrmann

            Comment

            • SJ Carter

              #7
              Re: JavaScript Validation Works In Firefox But Not IE

              On May 14, 4:00 pm, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
              wrote:
              SJ Carter wrote:
              On May 14, 3:30 pm, Joost Diepenmaat <jo...@zeekat.n lwrote:
              Joost Diepenmaat <jo...@zeekat.n lwrites:
              >You probably really shouldn't have a form element named "name".
              or if you do, you can probably use form.elements.n ame.value instead of
              form.name.value
              >
              Thanks Joost, but it still isn't working.
              >
              Post the error message, then.
              >
              I tried changing the form element name with no success, and then tried
              using the "form.elements. ...", with identical results.
              >
              DOM terms collide with markup terms here. Just to clarify, you should not
              have a form *control* named "name", i.e. you should not have
              >
              <input name="name" ...>
              >
              because the form *element*
              >
              <form ... name="foo">
              ...
              </form>
              >
              would then cause formRef.name to be evaluated to "foo" instead of the input
              object. Modifying or removing the `name' attribute value of the `form'
              element would not change that, which could explain what you observed.
              >
              You have to change the value of the `name' attribute of the `input' element
              instead. BTW, "submit" is another name that should be avoided, as form
              objects have a submit() method.
              >
              Please trim your quotes.
              >
              PointedEars
              --
              realism: HTML 4.01 Strict
              evangelism: XHTML 1.0 Strict
              madness: XHTML 1.1 as application/xhtml+xml
              -- Bjoern Hoehrmann
              Got it working. Turns out it wasn't even that function itself, but
              some embedded object that was messing things up. Thanks for all your
              help!

              Comment

              • SAM

                #8
                Re: JavaScript Validation Works In Firefox But Not IE

                SJ Carter a écrit :
                On May 14, 3:30 pm, Joost Diepenmaat <jo...@zeekat.n lwrote:
                >Joost Diepenmaat <jo...@zeekat.n lwrites:
                >>You probably really shouldn't have a form element named "name".
                >or if you do, you can probably use form.elements.n ame.value instead of
                >form.name.valu e
                >
                Thanks Joost, but it still isn't working. I tried changing the form
                element name with no success, and then tried using the
                "form.elements. ...", with identical results.

                Give another name than 'name' to your element would fix the trouble.

                but the right use to get an element named 'name' and from the form named
                'form1' is :

                document.forms['form1'].elements['name']
                or :
                document.form1['name']

                It is aways a good idea to do not use generic terms to name something
                (here 'name' is an attribute and IE can misunderstand of what name you talk)

                --
                sm

                Comment

                • GTalbot

                  #9
                  Re: JavaScript Validation Works In Firefox But Not IE

                  On 14 mai, 18:36, SJ Carter <sjmcar...@gmai l.comwrote:
                  Thanks Joost, but it still isn't working. I tried changing the form
                  element name with no success, and then tried using the
                  "form.elements. ...", with identical results.
                  Using Web Standards in your Web Pages
                  Accessing Elements with the W3C DOM
                  The MDN Web Docs site provides information about Open Web technologies including HTML, CSS, and APIs for both Web sites and progressive web apps.


                  will for sure give you the help you are seeking.

                  Gérard

                  Comment

                  Working...