开发者

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 :inpu开发者_运维知识库t').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


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜