Question

I have a form with a number of inputs that are validated by jquery validate.

I need to validate this form in two different ways - when the form is submitted and when the form is saved. These two actions have different requirements. When the form is submitted validate needs to check that all required fields are completed. However when the form is saved, I would like it to ignore the required rules, whilst still checking other rules (maxLength/number/email etc).

I have found the ignore aspect of jquery. Can this be added / removed ? If so, can it be used to target only the required rule? e.g. field has rules: required: true, maxlength: 100 . Field is still checked for maxlength but required is ignored.

Était-ce utile?

La solution

The only way to toggle rules dynamically is through the .rules('add') and .rules('remove') methods provided by the plugin.

Since you didn't show code, I can only show generic examples:

Remove required rule from the input with name="myfield"

$('input[name="myfield"]').rules('remove', 'required');

Remove required rule from all input elements that are type="text". Multiple elements require the .each() method…

$('input[type="text"]').each(function() {
    $(this).rules('remove', 'required');
});

Adding rules dynamically is a similar process.

$('input[name="myfield"]').rules('add', {
    required: true
});
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top