#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    236
    Rep Power
    12

    akk... Javascript and Mozilla :(


    Hi all, doing some form validation, works in IE but wont work in mozilla.

    My function:

    function validate()
    {
    if(uploadcat.category.value == -1)
    {
    alert("Error: This category does not allow uploads. Select another");
    return false;
    }
    else
    {
    return true;
    }
    }
    My form tag:

    <form method=post action="usercp.php" enctype="multipart/form-data" onSubmit="return validate()" name=uploadcat>

    Yes, i am sending files via this form, dont worry

    In IE, id the drop box value is -1, I get the error and the form is not submit, in mozilla, no error and it just submits

    Just incase, also include my select statement

    As can be seen, the first element is set to -1

    <select name="category" size="1">
    <option value="-1" class=bg> Action</option>
    <option value="2" class=bg> Sports Cars</option>
    <option value="14" class=>-- Corvette</option>
    <option value="6" class=>-- Dodge Viper</option>
    <option value="9" class=>---- To Delete</option>
    <option value="15" class=>------ Some test</option>
    <option value="17" class=>-------- An other level 4</option>
    <option value="16" class=>-------- Some other test</option>
    <option value="12" class=>-- Lexus</option>
    <option value="13" class=>-- Mustang</option>
    <option value="3" class=bg> Wild Wild West</option>
    <option value="-1" class=>-- Wayne</option>
    <option value="18" class=>---- Sub Cat of wayne</option>

    </select>
  2. #2
  3. Cobol Victim
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Location
    Munich, GER
    Posts
    124
    Rep Power
    12

    different types of variables?


    Perhaps mozilla doesn't compare variables as IE does.
    You compare a string "-1", submitted in the form with a number (uploadcat.category.value == -1).
    Try changing this:
    if(uploadcat.category.value == "-1")

    This should work
    Lebowski: Isn't that what makes a man?
    Dude: Mmm, sure. That and a pair of testicles.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Location
    Bavaria, Germany
    Posts
    140
    Rep Power
    12
    The problem is that your form tag just have a "name" attribute - Mozilla is more strict than IE and elements with a name attribute aren't available as global variable in scripting blocks.

    Use the forms collection of your document in order to get our form and the its fields collection to get the field:

    if (document.forms["uploadcat"]["category"].value == -1) {
    ...
    }

    And btw, in your mozilla, type as url: "javascript:" in order to see which error you get ;-).
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Location
    Barcelona
    Posts
    133
    Rep Power
    12
    no no, you're using bad code, which mozilla doesnt like:

    <form method=post action="usercp.php" enctype="multipart/form-data" onSubmit="return validate()" name=uploadcat>

    should be

    <form method="post" action="usercp.php" enctype="multipart/form-data" name="uploadcat">

    see the quotes, and also that onSubmit="return validate()" should be passed through the input button type submit.

    Then, also:
    class=bg> or class=>
    are incorrect. Use quotes there:
    class="bg">
    and if you will not assign a class, dont use it but dont assign it empty.

    I hope this fix it.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Location
    Bavaria, Germany
    Posts
    140
    Rep Power
    12
    The HTML specificiation allows attributes to be w/o "", but you shouldn't use it. (this really sucks, everyone should be forced to include strict.dtd in its html files ;-))

    To move the onSubmit attribute to the input button isn't a good idea since your form can be submitted by a numerous ways (e.g. through javascript: document.forms["name"].submit() or through just pressing enter in a textfield and then your button won't be pressed).

    This is the first time that i see onSubmit on a <input /> element, even the DOM specification sais nothing about having the onSubmit event called at the input button:

    submit
    The submit event occurs when a form is submitted. This event only applies to the FORM element.
    Bubbles: Yes
    Cancelable: Yes
    Context Info: None
    http://www.w3.org/TR/DOM-Level-2-Events/events.html
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    236
    Rep Power
    12
    Originally posted by Wingman
    The problem is that your form tag just have a "name" attribute - Mozilla is more strict than IE and elements with a name attribute aren't available as global variable in scripting blocks.

    Use the forms collection of your document in order to get our form and the its fields collection to get the field:

    if (document.forms["uploadcat"]["category"].value == -1) {
    ...
    }

    And btw, in your mozilla, type as url: "javascript:" in order to see which error you get ;-).
    Sorry I didnt reply sooner, however this works perfectly.

    Thanks again to all who replied.

    Taz
  12. #7
  13. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    Just a note: you don't need to use the forms[] collection to reference a named form - all named forms (like images and some other elements) are accessible as document properties:

    if (document.uploadcat.category.value == -1)

    ...etc. Internet Explorer exposes references to almost anything named/id'd as global variables, clogging up the window's namespace needlessly. You can also use the DOM-compliant document.getElementById() with a form having a id set.

IMN logo majestic logo threadwatch logo seochat tools logo