Hi,

I am using the Validate() plugin for various forms and I run into  
this issue with the depends method on various places.

So far I found only two references to dealing with depends:

http://docs.jquery.com/Plugins/Validation/validate

$(".selector").validate({
    rules: {
      contact: {
        required: true,
        email: {
          depends: function(element) {
            return $("#contactform_email:checked")
          }
        }
      }
    }
})

http://dev.jquery.com/ticket/2456

billingAddress: {
   required:true,
   minlength:5
   equalTo: {
     param: "#shippingaddress",
     depends: "#compare:checked"
   }
}

first of all non of these work for me so there must be something  
going on with them. but my real problem is that I want validation  
depending on fields other then checkboxes.

1. I only want to require zip code if the selected country from a  
drop down list is USA.

something like this:

zip: {
        required: {
                depends: "#countrycode:1"
                }
        },

where countrycode represents the country selector and 1 is the value  
for USA

2. in a from for password change I only want to require current  
password if new password is entered.
something like this:

current_password: {
        minlength: 6,
        required: {
                depends: "#new_password"
                }
        },

with this one I'd assume that if new_password is not empty then set  
the rule.

I also found this page:
http://dev.distilldesign.com/log/2008/mar/16/extending-jquery-form- 
validation-plugin/
http://lab.distilldesign.com/jquery-form-validation/extending/

this one works, though he uses checkboxes as well. here he basically  
extends the validate() plugin:

$.validator.addMethod('dependsOn', function (value, el, params) {
                                return !$(params.el).is(params.being) || 
$(el).is(':filled');
                        }, 'This field is required.');

then he uses the new method in the rule:

emailDependent: {
        dependsOn: {
                el: '#emailMethod',
                        being: ':checked'
                },
        email: true
},


I am not a javascript wizard so this method writing is getting my  
head dizzy. Can anyone tell me if there is a simple way of using  
depends in rules for validate() plugin?

Thanks,
Luke

Reply via email to