I have the following JavaScript function:
$('div.nextStepBilling').click(function (e) {
if ($(this).parent().parent().attr("id") == "newCardDiv") {
$('form#myForm').validate({
rules: {
cardHolder: "required",
cardNumber: {
required: true,
minlength: 15,
maxlength: 16
},
CVV: {
required: true,
minlength: 3,
maxlength: 4
}
},
messages: {
cardHolder: "Card holder name is required",
cardNumber: {
required: "Credit card number is required",
minlength: "Credit card must be at least 15 digits long",
maxlength: "Credit card must be at most 16 digits long"
},
CVV: {
required: "CVV (security code) is required",
minlength: "CVV must be at least 3 digits long",
maxlength: "CVV must be at most 4 digits long"
}
}
});
if ($('form#myForm').valid()) {
alert("valid");
} else {
alert("invalid");
}
}
});
When I click on the proper div.nextStepBillingit always alerts valid, even if there is nothing in those inputs.
I don't get any errors in the console and I can get jquery validate to work using $('form#myForm').validate().element("#cardHolder"); I have the latest jquery and jquery validate packages included (in proper order) on the page. Can anyone see anything wrong with the above code?