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 14 of 14
  1. #1
    Regular Coder
    Join Date
    Sep 2002
    Location
    South East UK. 35 miles east of London, in sight of the River Thames.
    Posts
    300
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Validate two Forms with one script

    I have two similar forms on a web site that that can be validated by one javascript. How do I pass the name of the form to the script and then test which form is calling it?

    Regards

    Gary

  • #2
    Regular Coder
    Join Date
    Nov 2002
    Posts
    596
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You don't generally pass a form's name to a script - you pass the Form object directly...more details as to the situation will get you a better answer.

  • #3
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Normally, you do as cheesebagpipe just said - you pass a reference to the form object, not the name of it. This you do by putting an onsubmit attribute on the form tag, like this: <form [...] onsubmit="return fnValidateForm(this);">

    The script:
    Code:
    function fnValidateForm(form){
       /*\
        |  In here, you access the form elements
        |  by form.elements[[string nameOfFormElement]].
        |  You return true if you want to continue with the
        |  submission, or false if you want to abort it.
       \*/
    }
    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

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [pimp] ya, like fValidate [/pimp]

    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”

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    [ho'] Hey, don't steal my glory! [/ho']

    If there's two things I've never been good at in JavaScript, it's form handling, including validation, and frames.
    Last edited by liorean; 03-31-2003 at 09:09 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

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

    Well good, then my next fValidate should be your cup of tea. It's obect oriented and will handle complex validations easily
    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”

  • #7
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    (Well, I don't like to use other's code - they seldom write my style. Noi, just truth)


    On the other hand, object orientation is something I'm good at, and regex's are also in my field of speciality.
    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

  • #8
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No insult taken. In fact, if I do borrow some code, the first thing I do is 'clean' it to my liking/style/taste/whatever
    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”

  • #9
    Regular Coder
    Join Date
    Sep 2002
    Location
    South East UK. 35 miles east of London, in sight of the River Thames.
    Posts
    300
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Hi Guys,

    Thanks for the advice but I haven't explained myself very well. Here goes again.

    I have two forms to validate. A Quote form to submit to a database and an Edit form to recall and modify the stored data.

    The button (1), at the end of the Quotation Form calls the function (2) setstatusandsubmit which sets the hidden field "strcurrentstatus" to hold the value 'continue'.

    The same button (1a), at the end of the Edit Form calls the function (2) setstatusandsubmit which sets the hidden field "strcurrentstatus" to hold the value 'saveandexit'.

    Once this is done, a function submitIt(formname) is called and, being part of an 'included' file is found and processes the 'Form' on the quotation page called 'formname'.


    1. <INPUT TYPE=BUTTON NAME="btnsubmitquote" onclick="setstatusandsubmit('continue');">

    1a. <INPUT TYPE=BUTTON NAME="btnstoreedit" onclick="setstatusandsubmit('saveandexit');">

    2. function setstatusandsubmit(strcurrentstatus) {
    formname.currentstatus.value = strcurrentstatus;
    submitIt(formname);
    }

    I need to test the value of (formname) as the first part of the validation and set a variable 'form' to equal either 'quote' or 'edit'. I can then select which set of form elements to test and validate.

    Hope this makes sense.

    Regards

    Gary

  • #10
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Try using this as the argument sent from the buttons, and retrieving the name from that attribute:
    Code:
    function setandsubmit(elm){
        switch(elm.name){
            case 'btnsubmitquote':
                /* run functions or expressions
                related to quotes here*/
                break;
            case 'btnstoreedit':
                /* run functions or expressions
                related to editing here*/
                break;
        }
        elm.form.submit(); // if this is what you want to do.
        return true; // Unless you want to cancel the event
    }
    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

  • #11
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Hmm, wait a moment - I just reread what you wrote up there - do you mean you have two different forms as in that you have two different form elements?

    If so, why don't you put these submit buttons inside their respective forms, and change their type attribute to submit?
    You really shouldn't have submit buttons that aren't really submit buttons, but if you have, you should at least put them in their respective forms.
    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
    Regular Coder
    Join Date
    Nov 2002
    Posts
    596
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I got a bit of a headache reading the above; could be just my head but the program structure you've outlined seems convoluted. Anyway: why not just pass the button (Input) object - as mentioned - and let the function figure it out?

    1. <INPUT TYPE=BUTTON NAME="continue" value="Continue" onclick="setstatusandsubmit(this);">

    1a. <INPUT TYPE=BUTTON NAME="saveandexit" value="Save and Exit" onclick="setstatusandsubmit(this);">

    function setstatusandsubmit(oButton) {
    var oForm = oButton.form;
    oForm.currentstatus.value = oButton.name;
    submitIt(oForm);
    }

    Stuck some data in the button's name property. Lost you here:
    I need to test the value of (formname) as the first part of the validation and set a variable 'form' to equal either 'quote' or 'edit'.
    Would help to know what you're validating (and what routine you're using).

  • #13
    Regular Coder
    Join Date
    Sep 2002
    Location
    South East UK. 35 miles east of London, in sight of the River Thames.
    Posts
    300
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Thanks liorean,

    I think I follow this. Time for me to research 'case' and 'switch'

    Regards

    Gary

  • #14
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Oh, it's just a way to write a multiple conditional (a'la if...elseif...else) that compares to the same object. What it in effect does is:

    1. Evaluate the argument of switch
    2. Compare the label (doesn't have to be a string) of the case to that argument.
    3. Execute case if match, jump to next case if not.
    4. If case executes and it includes a break statement, terminate the switch, when encountering the break statement otherwise repeat from 2.
    5. If last case has passed but there is a default label left, execute default.

    What I wrote is a more effective way of writing:
    Code:
    if(elm.name=='btnsubmitquote'){
        /*quote-related functionality */
    }else if(elm.name=='btnstoreedit'){
        /*edit-related functionality */
    }
    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
    •