One form, two submit buttons w/ different javascript validation

Posted on 2006-04-03
Last Modified: 2008-01-09
This might be pretty simple, but its been bugging me all day. I have a form that
can be submitted in two states, therefore requiring two differnt validation scripts.
As well, each submit needs to go to a differnt action page :).

Right now, I have submit buttons w/ onClick handelers to send the user to the right page.

I can't seem to get the validation to work properly. Is it possible to pass a variable when the
submit is pressed and there for run only a portion of the validation (if (var is defined) {run validation}; etc?
As well, what would my onSubmit look like?

Question by:twanlass
    LVL 52

    Accepted Solution

    You might be better off replacing your Submit buttons with standard buttons. Then using the onclick handler and validation routines to actually submit the form:

      <input type="button" name="Submit1" value="Submit1" onclick="ValidateAndSubmit('list or parameters');" />
      <input type="button" name="Submit2" value="Submit2" onclick="ValidateAndSubmit('list or parameters');" />

      <script language="javascript">
        function ValidateAndSubmit( arg1, arg2, etc )
            if (arg1 == 'Something')
                 // Set form "action" and validate based on button 1
                 // Set form "action" and validate based on button 2

            // Submit the form
    LVL 14

    Assisted Solution

    You can use only one submit button, as shown in the following example:


                <script language="javascript">
                      function decide(s)
                            if (s=="Hello")
                                  //validate using first method
                                  alert("You didn't write Hello");
                                  //validate using second method
                      <input type="text" name="text1"/>
                      <input type="submit" value="Say" onclick="decide(text1.value);"/>


    The JavaScript function should receive whatever parameters that will be used to determine the validation method.
    Next, the JavaScript code will then determine the validation method to use according to the value(s) of the parameter(s) passed to the function.


      Nayer Naguib

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Changing the installation path of this MSI 5 76
    sumHeights2  challenge 7 51
    How to split this in C++ 4 52
    changeXy challenge 13 40
    Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now