Reputation: 3712
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) || ($('#email1').val() != $('#email2').val())))
return false;
}
else
{
return true
}
}
How should I do this?
Thank you in advance!
Upvotes: 0
Views: 216
Reputation: 1607
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
}
});
Upvotes: 0
Reputation: 37633
$('#idOfForm').submit(function () {
if ($('#email1').val() != $('#email2').val()) {
// display error
return false; // Prevent form submitting
}
})
Upvotes: 2
Reputation: 15390
I would consider using the jQuery Validate Plugin You can use rules like equalTo()
to make your life much easier.
Upvotes: 0