1

On submit I do this:

$('#add-form').submit(function(e){ e.preventDefault(); if($('input[name=published]').is(':checked')) p.validateImgs(); //stop if p.validateImgs returns false 

Validate Imgs Function:

p.validateImgs = function() { //Check Thumb var thumb = $('#thumb li .img-drop-zone'); if(!thumb.has('img').length)thumb.addClass('error'); //Check Galery var gallery = $('#gallery li .img-drop-zone').first(); if(!gallery.has('img').length)gallery.addClass('error'); //return true/false }; 

How can I make it if false is returned from validateImgs, the submit script stops?

0

4 Answers 4

2

Since e.preventDefault() prevents the form from submitting, how about

$('#add-form').submit(function(e){ if($('input[name=published]').is(':checked') && !p.validateImgs()) e.preventDefault(); }); 

If you want to exit the function, use return as Jonathan mentions.

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

Comments

0

In validateImgs return true or false and capture the response in your submit function. Example.

$('#add-form').submit(function(e){ e.preventDefault(); if($('input[name=published]').is(':checked')) { response = p.validateImgs(); // if any error if(!response) return false; // stop function } 

Comments

0

I would use Johan's code, but just add a little modification:

$('#add-form').submit(function(e){ if($('input[name=published]').is(':checked') && !p.validateImgs()) { e.preventDefault(); return false; } }); 

Please notice the "return false" y added, just to be careful

Comments

0

You can exit a function early with a return statement.

Since it's conditional, place it under the if with p.validateImgs() as part of the condition:

if($('input[name=published]').is(':checked') && !p.validateImgs()) return; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.