Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New to the CF scene
    Join Date
    Feb 2004
    Location
    UK
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Validation Script won't work in Mozilla! Why?

    Hi I hope someone can help me,

    I'm a complete Javascript novice and I have pieced together a validation script for a form using examples.

    I have attached both the webpage and .js file for anyone to take a look at... My problem is that the script only seems to work, (produce an error message when an invalid selection is made), in Internet Explorer and Opera. When using any varient of Mozilla, such as Netscape or FireFox the script fails to operate.

    The beginning of the Form in the HTML page looks like this:

    <form name="rqstForm" action="./makerequest.pl" onSubmit="validate();" method=GET>

    and the Javascript is:

    function validate() {
    checkProgramme();
    checkCapacity();
    checkWeeks();
    }

    function checkProgramme() {
    if (rqstForm.programme.value != "select") {
    }
    else {
    alert('You must select a programme.');
    return false;
    }
    }

    function checkCapacity() {
    if ((rqstForm.capacity.value >0) && (rqstForm.capacity.value <501)) {
    }
    else {
    alert('Capacity must be between 1 and 500.');
    return false;
    }
    }

    function checkWeeks() {
    if (rqstForm.endweek.value >= rqstForm.startweek.value) {
    }
    else {
    alert('The end week must be later than the start week.');
    return false;
    }
    }

    I appologise for the untidyness of the HTML, however it was put together in a bit of a hurry and is dynamically generated by a Perl file when accessed online. The beginning of the Form is shown by a comment in the HTML file to make it easier to find.

    If someone could help me fix this problem it would be very much appreciated!

    Thank you.
    Last edited by Daedalus; 02-12-2004 at 07:12 PM.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    You are referencing the form incorrectly which IE allows
    This is the correct way:

    document.rqstForm

    but you can pass the form reference from the onsubmit using the keyword this which refers to the current element that is the form.

    <form onsubmit="return validate(this)"

    and you must return true or false to the form onsubmit handler to determine if submission will continue or not.


    function validate(objForm) {
    return checkProgramme(objForm) && checkCapacity(objForm) && checkWeeks(objForm);
    }

    function checkProgramme(objForm) {
    if (objForm.programme.value != "select") {
    ...

    Then each of the functions inside validate() must return true or false. true denotes no input error, while false otherwise.

    Check the javascript console so that you can see the details of the error.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    New to the CF scene
    Join Date
    Feb 2004
    Location
    UK
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you very much!

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by glenngv
    You are referencing the form incorrectly which IE allows
    This is the correct way:

    document.rqstForm

    ...
    Must have been in a hurry when you posted that one .


    Daedalus,

    The correct forms are document.forms.rqstForm or document.forms['rqstForm']. Either is valid and will work cross browser, the reason IE accepts the document.rqstForm and rqstForm by itself is because it pollutes the global namespace with every object that's given a name or an id. Excepting Opera which is trying to be as compatible as possible the rest of the browsers keep the global namespace clear since all those objects have proper places in the object model where they can be found.
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #5
    New to the CF scene
    Join Date
    Feb 2004
    Location
    UK
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you, very much appreciated... haven't had a chance to edit things yet, but I will do it tonight.

  • #6
    New to the CF scene
    Join Date
    Feb 2004
    Location
    UK
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks guys, got it working perfectly now!

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Originally posted by Roy Sinclair
    Must have been in a hurry when you posted that one .
    No.
    document.rqstForm is also valid and working. Though using the forms collection is the more proper way.

    Try this simple example and then tell me which browser this is not working.

    <html>
    <body>
    <form name="myform">
    <input type="button" value="What is document.myform?" onclick="alert(document.myform)" />
    </form>
    </body>
    </html>
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #8
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I stand corrected. It works in all the browsers I have. I probably won't use it myself since adding the .forms. does make it clear what I'm trying to use.
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #9
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Glenngv: Yeah, but how about something like this?

    <form id="whatever">
    &nbsp;&nbsp;<input name="whatever">
    </form>

    <form id="whatever">
    &nbsp;&nbsp;<input name="forms" id="whatever">
    </form>

    <form name="whatever">
    &nbsp;&nbsp;<input name="whatever" id="forms">
    </form>

    <form name="elements">
    &nbsp;&nbsp;<input name="elements" id="forms">
    </form>



    (Note that for the form element the id and name attributes share namespace, but for the form controls id and name have separate namespaces. Id is still limited to one per document, but name is limited to a single control or set of controls within the same form element)
    Last edited by liorean; 02-13-2004 at 05:30 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #10
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Furthermore, liorean, XHTML doesn't even allow the name attribute for form elements.
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #11
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Well, Strict doesn't. Transitional and Frameset does.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #12
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Right

    Of course, I made fValidate for a reason
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #13
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Oh, a "Welcome back!" might be appropriate. I've missed our occasional discussions here.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •