views:

56

answers:

3

I am trying to validate my form using two separate JavaScript functions:

<form onsubmit="return formCheck(this); return validate_dropdown();"  
      action="somepage.php" 
      method="post" 
      name="something">

When I add just one on submit value, each function works fine individually, when I add two only the first function's validation works, not the second. What is wrong here?

+3  A: 

Remove the returns from the onsubmit and add them to the functions:

onsubmit="return (formCheck(this) && validate_dropdown())"

Aaron Harun
lavinio
More simple to type, yes. But it is not as quickly understandable for a beginner.
Aaron Harun
+2  A: 
onsubmit="return (formCheck(this) && validate_dropdown())"
BrunoLM
A: 

Your code will not work, because, as soon as the first function (formCheck) is executed, it will return a value, which will either allow or deny form submission.

If you have two or more functions you want to use at the same time, you can either combine their results or write a new function, which in turn will run both your validation functions and return the results.

Method A.

<form onsubmit="return (function1(this) && function2(this))">

Method B.

<script type="text/javascript">
function formValidation(myForm)
{
    var result =  function1(myForm);
    result = result && function2(myForm);
    return result;
}
</script>

<form onsubmit="return formValidation(this)">
Anax