Specify validator order in jQuery validate plugin

I am wondering if it is possible to specify the execution order of validators.

Currently, I have written a special validator that checks that it is [a-zA-Z0-9] +, to make sure that the login checks our rules and the remote validator, to make sure that the login is available, but is currently remote The validator is running in front of my custom validator. I would like to start the remote validator only if the element validates my custom validator.

Any clue?

thank you for reading

+6
source share
3 answers

You need to patch / rewrite / render monkeys harmless (depending on which term suits you best) $.fn.rules , which uses the validate plugin when creating rules to run for a field, in order to put its rule in second place in the rules object , after the required rule.

If you are looking for rules in a script, you need to add similar logic to it before if (data.required)

 If (data.yourRuleName) { var param = data.yourRuleName; delete data.yourRuleName; data = $.extend({ yourRuleName: param }, data); } 

Or you can copy the rules section, add above and put it in the script that is referenced after checking the script. That way, if the script changes, you don’t have to apply these changes to the new version of the script (assuming the rules work the same anyway).

+3
source

As far as I understand, the rules for this element are processed in the order in which they appear. And since version 1.12 , you must first process the rule "required", and the "remote" last.

0
source
-1
source

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


All Articles