Tutorial :New Function: How to iterate through all form elements before returning true or false


I have a new function that I will be calling when the submit button is pressed for the form. I'm trying to use this validation, not a plug-in, for experience.
How would I iterate through all the forms, determine if they're all valid, before exiting out of the function. Though if they're all valid, return true and continue, otherwise if false, stay in the isFormValid function until all the forms are valid. Any ideas?

Here's what I've got so far.

function isFormValid() {    var valid = false;    $('form :input').each(function() {     var input = $(this);   label = $(input).prev();     if (!$.trim(input.val()).length) {     label.effect('pulsate', { times : 1 }, 400).addClass('required');     $(":input[value='']:not(.nofocus):visible:enabled:first").focus();     valid = false;   }   else if (input.attr('id') == 'email') {    if (!isValidEmailAddress(input.val())) {       label.effect('pulsate', { times : 1 }, 400).addClass('invalid')       input.val('');       $(":input[value='']:not(.nofocus):visible:enabled:first").focus();       valid = false;     }    }   });  }  



with more options, why not try to use this form validation plugin..


Got it. Only had to add this outside the each iteration and change the original var valid = true

if (!valid) {      return false;  }  else      return true;  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »