开发者

How do I compare two val()

I'm using JQuery to get values from a form. When I submit the form I want to make a check on that the user has enterd the same e-mail twice and that the format is correct of the email. I can't figure out how to do it. Here's my if-clause which firebug complains at:

   $('#subscribe_now').submit(function(event) {


    regExp = /^[^@]+@[^@]+$/;

       if(($('#email1').val().search(regExp) == -1) || ($('#开发者_如何学Cemail1').val() != $('#email2').val()))) 
             return false;
        }
        else
        {
             return true
        }
     }

How should I do this?

Thank you in advance!


$('#idOfForm').submit(function () {
    if ($('#email1').val() != $('#email2').val()) {
        // display error
        return false; // Prevent form submitting
    }
})


I would consider using the jQuery Validate Plugin You can use rules like equalTo() to make your life much easier.


You should use the submit event:

$('#your_form_id').submit(function(){

   var errors = Array(); // used to display or alert() errors

   if($('#email1').val() != $('#email2').val()) {
      errors.push("Email verification don't match");
   }

   // do something else, like checking user login length, ...
   // and add errors to the array

   // if there are errors, don't submit
   if(errors.length > 0) {
      // display error by inserting in html, or display with alert()
      return false; // cancel event
   }
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜