jQuery Validation: Changing Rules Dynamically

后端 未结 10 911
栀梦
栀梦 2020-12-07 13:07

I have a single form that, depending on which radio button is clicked (Login or Signup), displays either:

  • email address
  • password

or:

10条回答
  •  失恋的感觉
    2020-12-07 14:11

    This post is from years ago, but since it has been viewed a lot of times, I think it would be useful to say that the jQuery Validation plugin now allows us to read, add and remove rules.

    Some examples:

    1. Print all rules attached to #myField: console.log($('#myField').rules());

    2. Remove an unwanted rule: $('#myField').rules('remove', 'min');

    3. Add a new rule: $('myField').rules('add', {min: 10});

    So say if you want to dynamically update the minimum value required. You can call a remove and a add right after:

    // Something happened. The minimum value should now be 100
    $('#myField').rules('remove', 'min');
    $('#myField').rules('add', {min: 100});
    

    More information can be found here: https://jqueryvalidation.org/category/plugin/#-rules()

提交回复
热议问题