How to return from a function completely in JQuery -


i have form, in contains number of input fields. when user click submit button, validate user input. because form (suppose it's alike resume) contains few similar field, such year, school, diploma, etc. use jquery attribute selector choose these similar fields , use jquery each function iterate them validation. code below.

$("#submit_btn").click(function () {             $('input[name="education_year[]"]').each(function(){                 if (!$(this).val()) {                     alert("education year empty !");                     return;                 }             });              $('input[name="diploma[]"]').each(function(){                 if (!$(this).val()) {                     alert("diploma field empty");                     return;                 }             });         }); 

because user may leave several fields empty, want code gives out 1 alert. however, in code above, return keyword doesn't help. seems me more break statement exit each function. can tell me how end click function ?

try

$("#submit_btn").click(function () {     var valid = true;     $('input[name="education_year[]"]').each(function(){         if (!$(this).val()) {             alert("education year empty !");             valid = false;             return false;         }     });      if(!valid){         return;     }      $('input[name="diploma[]"]').each(function(){         if (!$(this).val()) {             alert("diploma field empty");             valid = false;             return false;         }     }); }); 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -