I’m trying to exit a function when a required input is not valid… but am having trouble. See below for code. Why doesn’t the return work? I’ve also tried return false and break with no luck.
Code:
function SendTo(id){
///////// VALIDATE & CLEAR INPUTS ////////////
var elems = $('form').find('input');
var arr = jQuery.makeArray(elems);
jQuery.each(arr, function() {
var input = $(this);
//REQUIRED
if (input.hasClass('required')) {
if (input.val() == '' || input.val() == input.attr('placeholder')) {
input.addClass('fail');
$('#messages .error').text('The above fields are required.', function() {
$(this).fadeIn('slow')
});
return;
}
}
//CLEAR
if (input.val() == input.attr('placeholder')) {
input.val('[EMPTY]');
}
});
}
EDIT:
I got it working with the following code:
///////// VALIDATE & CLEAR INPUTS ////////////
var allGood = true;
$('form input').each(function() {
var input = $(this);
//REQUIRED
if (input.hasClass('required')) {
if (input.val() == '' || input.val() == input.attr('placeholder')) {
input.addClass('fail');
$('#messages .error').text('The above fields are required.', function() {
$(this).fadeIn('slow')
});
allGood = false;
}
}
});
return allGood;
I think it’s because you’re doing jQuery.each.
Returing from that will just go to the next one. However, I’m pretty sure
return false;, like Minum said shouldbreak, notconintueFrom the website: