3

I'm trying to call multiple JavaScript functions. It submits even though my functions return false. I receive the alert messages but it submits anyway.

This is my form.

<form name="myform" id="myform" action="receipt.html" method="post" onSubmit="return allValidation();">
2
  • You aren't using the return values. Commented Nov 29, 2015 at 15:40
  • @SLaks I've edited the functions now. Something like that? Commented Nov 29, 2015 at 16:12

4 Answers 4

2

You aren't returning anything in allValidation, so that it will continue to next method execution. Try this,

function allValidation() {
    return validateForm() && selectCountry() && validateEmail();
}
Sign up to request clarification or add additional context in comments.

4 Comments

add default return true value(as last line) in all validateForm(), selectCountry(), validateEmail() methods.
Now it submits if validateForm is true and bypasses the other two functions
return true should be without bracket. I have modified your question with my suggestion. Hope this will resolve your issue. Keep trying..
Excellent! Thanks you so much.
2

You can use multiple functions and then and their results to get final validation. and also add return true statements in all functions where validation is successful

function allValidation()
   {
      return (validateForm() && selectCountry() && validateEmail());
   }

Comments

1

It's because your function always return true no matter what values return the validateForm() selectCountry() and validateEmail() functions.

Change your allValidation() function like this:

function allValidation()
{
    return validateForm() && selectCountry() && validateEmail();
}

2 Comments

This bypassed the selectCountry() and validateEmail() functions and submits if validateForm() works.
@dafyddgj , I don't think so.. I tested the code on Chrome 47, Firefox 42 and IE 8 and it worked on all of them. Javascript evaluates the functions one by one, starting from the left and stops on the first function that returns false (if any). In such case, allValidation() returns false and indeed, the rest of the functions are bypassed. If all functions return true, then the allValidation() returns true. So, using return validateForm() && selectCountry() && validateEmail(); or return (validateForm() && selectCountry() && validateEmail()); has exactly the same effect.
0
function allValidation(){
  return validateForm() && selectCountry() && validateEmail();
}

1 Comment

Please avoid code-only answers. Please add an explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.