Jennifer Anthony
Jennifer Anthony

Reputation: 2277

jQuery validation and check all together

I make a jquery validation, problem is here that this part from code first(first click on button) check selectbox and second(second click on button) fields. how can it fix as that check all together with one click on button?

Exampel(see full code): http://jsfiddle.net/JfPaN/

$('.submit').submit(function () {
    if (required_selectbox() == false || required_valid() == false) {
        return false;
    }
});

Upvotes: 0

Views: 76

Answers (1)

Paul
Paul

Reputation: 141827

Your problem is caused by Short-Circuit Evaluation on this line expression :

required_selectbox() == false || required_valid() == false

Javascript will not execute required_valid() if required_selectbox() returns false, since it already knows the result of the boolean expression as soon as the left side is true.

Here is one solution to your problem:

$('.submit').submit(function(){
    var passed = required_selectbox();
    passed = required_valid() && passed;
    if(!passed)
        return false;
});

Upvotes: 1

Related Questions