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. Advertising

  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. Advertising

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:
    393
    Teemu Keiski
    Oct 16, 2003
  2. Matt
    Replies:
    0
    Views:
    679
  3. Matt
    Replies:
    2
    Views:
    157
    Bob Barrows [MVP]
    Jun 27, 2004
  4. sandyde2
    Replies:
    2
    Views:
    177
    sandyde2
    Oct 2, 2003
  5. Matt
    Replies:
    1
    Views:
    194
    Thomas 'PointedEars' Lahn
    Jul 5, 2004
Loading...

Share This Page