How to add 'depends:' rule to many fields in jQuery validation plugin? -


i have used rule validate user billing address if wants bill (the input #billyes checked), current code:

                //billing validation                 legal_id: {                     required: {                         depends: function(element) {                           return $('#billyes').is(":checked");                         }                     }                  },                  billing_name: {                     required: {                         depends: function(element) {                           return $('#billyes').is(":checked");                         }                     }                  },                  billing_adress: {                     required: {                         depends: function(element) {                           return $('#billyes').is(":checked");                         }                     }                  },                   .... etc 

it repeated line return $('#billyes').is(":checked");

i want this:

                //billing validation                 legal_id,billing_name,billing_adress,etc: {                     required: {                         depends: function(element) {                           return $('#billyes').is(":checked");                         }                     }                  } 

is possible? there alternative?

thanks you.

legal_id,billing_name,billing_adress,etc: { 

within .validate() method, cannot @ all. object literal within rules option must key:value pairs key can single field name.

however, there way apply same rule multiple fields @ once:

you can combine .rules() method jquery .each(). you'll need clever jquery selector target them @ once. "starts with" selector or class. can't use "starts billing_" since have legal_id, you'll need assign class these fields or else.

$('.yourfields').each(function() {     $(this).rules('add', {         required: {             depends: function(element) {                 return $('#billyes').is(":checked");             }         }     }); }); 

otherwise, no, within .validate() method, there no shortcut.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -