Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove the 'required' rules from all form inputs in jQuery Validate?

As above, how can I remove all of the required rules from a form using jQuery Validate?

This is what I have at present:

var settings = $('form').validate().settings;

//alert(settings.rules.toSource());
for (var i in settings.rules){
    alert(i);
    delete settings.rules.i; 
    // ^ not sure about how to do this properly,
    // and how to just delete the 'required' rule and keep the rest intact
}

The function native to jQuery Validate returns an undefined error:

$("input, select, textarea").rules('remove','required');
like image 451
bcmcfc Avatar asked Jul 23 '10 11:07

bcmcfc


People also ask

How do I remove a validation rule?

To remove a certain data validation rule, select any cell with that rule, open the Data Validation dialog window, check the Apply these changes to all other cells with the same settings box, and then click the Clear All button.

Does jQuery validate require a form?

The jquery validate plugin requires a form element to function, so you should have your form fields (no matter how few) contained inside a form. You can tell the validation plugin not to operate on form submission, then manually validate the form when the correct submit button is clicked.


2 Answers

And if you don't want to mess with validate's internal settings, you can use the public function in this way:

$('input, select, textarea').each(function() {
    $(this).rules('remove', 'required');
});

Note that leaving out the second parameter (required) would remove all the rules, not just the "required".

like image 102
Christophe Deliens Avatar answered Oct 14 '22 00:10

Christophe Deliens


As can sometimes be the case, I appear to have stumbled on the answer just after resorting to posting on here.

for (var i in settings.rules){
   delete settings.rules[i].required;
}

Works.

like image 23
bcmcfc Avatar answered Oct 14 '22 01:10

bcmcfc