jQuery form validation using each() not working as expected -


i trying create own simple form validation while looping through form elements class '.required' using each().

the problem is, keeps breaking out of loop , jumps 'return true;' if fields empty.

this html:

<input type="text" id="personal-name" name="personal-name" class="required-field" placeholder="first name"> 

and jquery code:

function validform() {      var privacyapprove = $('#privacy-approved');     var errorwindow = $('.errors');      // fade out error after 3 seconds     var errorfadeout = setinterval(function () {         errorwindow.fadeout();         clearinterval(errorfadeout);     }, 3000);      if (!privacyapprove.is(':checked')) {         errorwindow.fadein().text("יש להסכים לתנאי התקנון");         return false;     }      $('input.required-field, textarea.required-field').each(function() {             if ($(this).val() == 0) {                 errorwindow.fadein().text("אנא השלם את כל הפרטים הנדרשים המודגשים באדום");                 $(this).addclass('input-error');             } else {                 $(this).removeclass('input-error');             }     });      return true; } 

what doing wrong??

thanks! :)

it seems want

if ($(this).val() == '') 

instead of

if ($(this).val() == 0) 

a valid alternative be

if ($(this).val().length == 0) 

also, should avoid returning true always. change return value this

return $('.input-error').length == 0 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -