在jQuery validate插件中指定验证器的顺序 [英] Specifiy the order of the validators in jQuery validate plugin

查看:297
本文介绍了在jQuery validate插件中指定验证器的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以指定验证器的运行顺序.

I'm wondering if it's possible to specify the order in which the validators are run.

当前,我编写了一个自定义验证器,用于检查它是否是[a-zA-Z0-9] +以确保登录名可以验证我们的规则,并提供一个远程验证器来确保该登录名可用,但当前已启动远程验证器在我的自定义验证器之前.我只想在元素验证我的自定义验证器时启动远程验证器.

Currently I wrote a custom validator that check if it's [a-zA-Z0-9]+ to make sure the login validates our rules and a remote validator to make sure that the login is available but currently the remote validator is lauched before my custom validator. I would like to launch the remote validator only if the element validates my custom validator.

有任何线索吗?

感谢阅读

推荐答案

您需要对验证插件在创建规则时使用的$.fn.rules进行扩充/修改/覆盖(无论哪个术语对您来说最有意义).在required规则之后运行一个字段以将您的规则推到规则对象的第二位.

You need to augment/monkey patch/overwrite (whichever term makes the most sense to you) the $.fn.rules that the validate plugin uses when creating the rules to run for a field to push your rule to second place in the rules object, after the required rule.

如果您在脚本中搜索rules,则需要在if (data.required)

If you search for rules in the script, you'll need to add similar logic to this just before if (data.required)

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

或者您也可以复制rules部分,添加以上内容并将其放入在validate脚本之后引用的脚本中.这样,如果脚本更改了,您将不必将这些更改应用于新版本的脚本(假设规则仍将以相同的方式起作用).

Or you can copy the rules section out, add the above and put it in a script that is referenced after the validate script. This way, if the script changes, you won't have to go applying these changes to the new version of the script (assuming that rules would still work the same way).

这篇关于在jQuery validate插件中指定验证器的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆