How to use jquery validation "depends" on rules other than "required"?

Each “depends” example that I can find on the Internet uses the “required” rule. Clearly, this is not enough. How can I use the "depends" clause, for example, with the "regex" rule?

I have two radio buttons, if one of them is selected, I regex-check the text box. If another is selected, I don’t care what is in the text box.

+6
source share
1 answer

The second example in the documentation uses email :

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

In general, this rule says that “contact is required and it must be an email address if #contactform_email checked”.

So, if you want to do this with a regular expression (or any parameter that requires a rule rule), it looks like this:

  minlength: { param:6, depends: function (element) { return $('#len').is(':checked'); } } 

That says: "The minimum length is 6, but only if the #len flag is #len ."

See here: http://jsfiddle.net/ryleyb/8Nsm3/

+12
source

Source: https://habr.com/ru/post/970316/


All Articles