1

I am trying to execute some code in order, but cannot get it to work. What I want to do is:

var validate = function($form) {
    $form
        .find('[data-required]')
        .each(function() {
            if(Util.isEmpty($(this).val())) {
                $(this).addClass('error');
                Util.Notify(Util.ERROR, $(this).data('required'));
                return false;
            }
            else {
                $(this).removeClass('error');
            }
        });
};

var submit = function($form) {
  $.post(actionUrl, $form.serialize(), function(response) {
    //...
  });
};

validate(); // must execute first with no False return
submit();   // must execute second and only if validate() not returns False

However, my $.post() gets executed even if validate() returns false. How can I force submit() to be executed after validate is executed and did not return false? I googled and tried a few example based on what people posted, but was unsuccessful.

4
  • 4
    validate doesn't return anything. Also you're not passing it any parameters even though it expects one. You need to have validate return something then you can do if(validate()){ submit(); } Commented May 23, 2013 at 21:24
  • 2
    First off, you're returning from .each's callback and not validate. Commented May 23, 2013 at 21:25
  • General formula should be if(validate()) { submit(); }. To make this work, validate should return true (validated) or false (not validated). Commented May 23, 2013 at 21:28
  • For a validation like this, you may wish to consider the Validation Plugin jqueryvalidation.org/documentation. Commented May 24, 2013 at 0:38

1 Answer 1

3

You could call submit() from within validate() when the conditions are correct, or you could do this:

if(validate()) {
    submit(); 
}

Just make sure validate() returns something when not false, so the if evaluates as true...

Also make sure that the return is on the right place, the way it is now, it's returning from the anonymous function inside .each(), as Fabrício Matté mentioned...

Sign up to request clarification or add additional context in comments.

3 Comments

Just a side note to this code - if submission is triggered by a submit type button the default behavior of the form needs to be prevented with preventDefault() (api.jquery.com/event.preventDefault) otherwise the form will be submitted even if its not valid.
Actually, it's only trying to return from the anonymous function inside .each(). In reality, returning anything falsy from an .each() callback is the same as break in a for loop - ie. the loop terminates immediately.
That was my error then. For some reason I thought that by returning false from each(), returns false from validate() too. I see it now. Thanks.

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.