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:
Updated Code
$('#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() != "" || $("#chDefZone").val() != "Select Zone")
{
$ev = 1;
$pv = 1;
$("#profSet").validate({
rules: {
chem: {
email: true
},
chpw: {
minlength: 6
},
chpw2: {
minlength: 6,
equalTo: "#chpw"
}
},
messages:{
chpw2: {
equalTo: "Passwords must be the same."
},
chpw: {
minlength: "Password must be at least 6 characters."
}
}
});
//validates an email if there is one, trips the valid variable flag
if($("#chem").val() != "")
{
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
}
});
Now the problem is that one of the invalidity flags was being tripped but I can't find where, the form should return valid under my tests.