I have a form, in which contains a number of input fields. When the user click the submit button, I would like to validate the user input. Because this form (suppose it's alike a resume) could contains a few similar field, such as year, school, diploma, etc. So I use JQuery attribute selector to choose these similar fields and use JQuery each function to iterate them for validation. My code is like below.
$("#submit_btn").click(function () {
$('input[name="education_year[]"]').each(function(){
if (!$(this).val()) {
alert("education year is empty !");
return;
}
});
$('input[name="diploma[]"]').each(function(){
if (!$(this).val()) {
alert("diploma field is empty");
return;
}
});
});
because the user may leave several fields empty, so i want my code gives out only one alert. However, in the code above, the return keyword doesn't help. It seems to me that it is more like a break statement to exit the each function. Can anyone tell me how to end the click function ?