I'm making a form that has the following validation rules: no fields are "required", but if you enter an email, it must be valid, and if you enter a password, it must be at least 6 characters and match the confirm password field value.
The form works flawlessly with no validation so I know it's not a PHP or HTML problem.
Here's the jQuery code handling the form:
$('#success').hide();
$('#pwerror').hide();
$('#emailError').hide();
$('#subSet').live('click',function() {
//if any of the fields have a value
if($("#chfn").val() != "" || $("#chln").val() != "" || $("#chpw").val() != "" || $("#chpw2").val() != "" || $("#chem").val() != "")
{
$ev = 1;
$pv = 1;
//validates an email if there is one, trips the valid variable flag
if($("#chem").val() != "")
{
$("#profSet").validate({
rules: {
chem: {
email: true
},
chpw: {
required: false,
minlength: 6
},
chpw2: {
required: false,
minlength: 6,
equalTo: $("#chpw").val()
}
},
messages:{
chpw2: {
equalTo: "Passwords must be the same."
},
chpw: {
minlength: "Password must be at least 6 characters."
}
}
});
if(!($("#profSet").valid()))
{
$ev = 0;
}
}
//if either password field is filled, start trying to validate it
if($("#chpw").val() != "" || $("#chpw2").val() != "")
{
if(!($("#profSet").valid()))
{
$pv = 0;
}
}
//if those two were valid
if($pv == 1 && $ev == 1)
{
$.post('php/profSet.php', $('#profSet').serialize(), function(){
$('#profSet').hide();
$('#success').show();
});
}
//if either was invalid, the error was already tripped, and this code exits here
}
});
The problem I'm having now is that the "passwords must be the same" error keeps getting triggered even if both fields are blank, or if they actually are the same. Therefore, the form cannot submit. Any help?