I want to return false or break the foreach loop. But it is not working. This is the code which i have tried.
function validate()
{
var validate="false";
var atLeastOneIsChecked = $("input[name='city[]']").is(":checked");
if(atLeastOneIsChecked==true){
$("input:checkbox[name='city[]']:checked").each(function(j){
var city=$(this);
$('select[name="category[]"]').each(function(i){
if(i==j){
if($(this).val()=="0"){
alert("Please select hotel category");
city.click();
//validate="false";
return false;
}
else
{
validate="true";
}
}
// return false;
});
alert(j);
// return false;
});
//return false;
}
else
{
alert('Please select atleast one city.');
validate="false";
}
if(validate=="true"){
return true;
}
else
{
return false;
}
}
After the "Please select hotel category" the function should return false. But the form is posted after the alert. I don't what's wrong.