Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1992
  • Last Modified:

One form, two submit buttons w/ different javascript validation

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?

Thanks!
0
twanlass
Asked:
twanlass
2 Solutions
 
Carl TawnSystems and Integration DeveloperCommented:
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
        }
        else
        {
             // Set form "action" and validate based on button 2
        }

        // Submit the form
    }
  </script>
0
 
nayernaguibCommented:
You can use only one submit button, as shown in the following example:

_____________________________________________

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

_____________________________________________

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
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now