0

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;
            }

        });
Sparky
  • 98,165
  • 25
  • 199
  • 285
ermSO
  • 325
  • 1
  • 2
  • 12
  • It's very unclear. What does your CRM have to do with anything? **Why** do you need to use `.rules('add')`? "Depends" on whether "State" is already filled out? You would not normally need the `.rules()` method for that kind of thing. Where is the rendered HTML for the `form`? – Sparky Sep 23 '17 at 22:32
  • What is `CRM.$`... is this rendered or server-side code? Only show the rendered JavaScript along with the rendered HTML of the form. – Sparky Sep 23 '17 at 23:17

0 Answers0