In my form the zipcode input comes before the State input. Therefore, I want to validate my zipcode only after my State input has been filled out. I have gotten the remote part to work. I have not been able to figure out how to use the "depends" of the jQuery Validation plugin. Due to the CRM that I am using, I need to use the .rules('add', {...}) way of setting up validation. Here is the code I am using:
CRM.$(".option-com-civicrm form[class^='CRM_'] #billing_postal-code-5, " +
".com_civicrm form[class^='CRM_'] #postal_code-1"
).rules('add', {
remote: function () {
var r = {
url: "http://localhost:81/example.org/media/plg_civcrm_frmvalidation/php/val-zip.php",
data: {
address2: function() {
return CRM.$(".option-com-civicrm form[class^='CRM_'] #billing_street_address-5, " +
".com_civicrm form[class^='CRM_'] #street_address-1"
).val();
},
city: function() {
return CRM.$(".option-com-civicrm form[class^='CRM_'] #billing_city-5, " +
".com_civicrm form[class^='CRM_'] #city-1"
).val();
},
state: function() {
return CRM.$(".option-com-civicrm form[class^='CRM_'] #billing_postal-code-5, " +
".com_civicrm form[class^='CRM_'] #state_province-1"
).val();
},
zipcode: function() {
return CRM.$(".option-com-civicrm form[class^='CRM_'] #billing_postal-code-5, " +
".com_civicrm form[class^='CRM_'] #postal_code-1"
).val();
},
},
type: "post",
depends: function(element) {
return CRM.$(".option-com-civicrm form[class^='CRM_'] #billing_postal-code-5, " +
".com_civicrm form[class^='CRM_'] #state_province-1"
).val() !== '';
}
};
return r;
}
});