Form validation w/ multiple buttons

Discussion in 'Javascript' started by Aaron C, Dec 4, 2004.

  1. Aaron C

    Aaron C Guest

    I'm validating a form that has two buttons ("Next" and "Back"). The call
    to the script is currently in the form's onSubmit() handler. Upon
    pressing either button, the script runs. This is understandable, since
    either button press is considered a submit. Is there a way to test which
    button was pressed to determine whether or not to run the script?

    Thanks,
    Aaron

    P.S. I've already tried attaching the same script to the "Next" button's
    onSubmit() handler, and when doing so, the script is apparently never run.
     
    Aaron C, Dec 4, 2004
    #1
    1. Advertisements

  2. Aaron C

    McKirahan Guest

    "Aaron C" <> wrote in message
    news:...
    > I'm validating a form that has two buttons ("Next" and "Back"). The call
    > to the script is currently in the form's onSubmit() handler. Upon
    > pressing either button, the script runs. This is understandable, since
    > either button press is considered a submit. Is there a way to test which
    > button was pressed to determine whether or not to run the script?
    >
    > Thanks,
    > Aaron
    >
    > P.S. I've already tried attaching the same script to the "Next" button's
    > onSubmit() handler, and when doing so, the script is apparently never run.


    One way is to set a variable via each Submit button's onClick then test it.

    <html>
    <head>
    <title>onsubmit.htm</title>
    <script type="text/javascript">
    var bool;
    function submits() {
    bool ? alert("Prev") : alert("Next");
    }
    </script>
    </head>
    <body>
    <form action="" method="post" onsubmit="return submits()">
    <input type="submit" name="Prev" value="<=" onclick="bool=true">
    <input type="submit" name="Next" value="=>" onclick="bool=false">
    <form>
    </body>
    </html>
     
    McKirahan, Dec 4, 2004
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. D
    Replies:
    2
    Views:
    480
    Teemu Keiski
    Oct 16, 2003
  2. dw
    Replies:
    6
    Views:
    2,818
    Joe Gass
    Sep 29, 2004
  3. Matt
    Replies:
    0
    Views:
    810
  4. Nathan Sokalski
    Replies:
    0
    Views:
    344
    Nathan Sokalski
    Jul 24, 2008
  5. Matt
    Replies:
    2
    Views:
    286
    Bob Barrows [MVP]
    Jun 27, 2004
  6. sandyde2
    Replies:
    2
    Views:
    317
    sandyde2
    Oct 2, 2003
  7. bnp
    Replies:
    4
    Views:
    617
  8. Matt
    Replies:
    1
    Views:
    323
    Thomas 'PointedEars' Lahn
    Jul 5, 2004
Loading...