views:

27

answers:

2

Hi there, 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;
   }
  }
 });
}

Thanks

A: 

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

Reigel
A: 

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;
Ryan
You could just write `return valid;`. It has the same meaning as code above.
Crozin
well, for that, you could just `return valid;` ;)
Reigel