【问题标题】:jQuery.validate ignore passwordjQuery.validate 忽略密码
【发布时间】:2012-09-20 12:48:46
【问题描述】:

我使用 jquery.validate.js 来验证我们网站上的表单。今天我将验证插件的版本更新到 v 1.9,从那时起我就遇到了现场密码验证的问题。

我找到了发生这种情况的原因,现在我正在寻找“正确”的方法来解决它。 在以前的版本中,方法属性规则如下:

attributeRules: function (element) {
            var rules = {};
            var $element = $(element);

            for (var method in $.validator.methods) {
                var value = $element.attr(method);                      
                if (value) {
                    rules[method] = value;
                }
            }    

            // maxlength may be returned as -1, 2147483647 (IE) and 524288 (safari) for text inputs
            if (rules.maxlength && /-1|2147483647|524288/.test(rules.maxlength)) {
                delete rules.maxlength;
            }

            return rules;
        }

在 1.9 版本中,它看起来有点不同:

attributeRules: function (element) {
            var rules = {};
            var $element = $(element);

            for (var method in $.validator.methods) {
                var value;

                // If .prop exists (jQuery >= 1.6), use it to get true/false for required
                if (method === 'required' && typeof $.fn.prop === 'function') {
                    value = $element.prop(method);
                } else {
                    value = $element.attr(method);
                }

                if (value) {
                    rules[method] = value;
                } else if ($element[0].getAttribute("type") === method) {
                    rules[method] = true;
                }
            }


            // maxlength may be returned as -1, 2147483647 (IE) and 524288 (safari) for text inputs
            if (rules.maxlength && /-1|2147483647|524288/.test(rules.maxlength)) {
                delete rules.maxlength;
            }

            return rules;
        }

据我了解,在以前的版本中,该方法没有检查输入元素的类型属性,也没有添加“密码”验证器。在 1.9 版中,它检查元素是否具有“密码”类型并添加验证器。

问题:
如何告诉 jQuery.validator 忽略“密码”类型的输入?

谢谢

【问题讨论】:

  • 描述您的问题。你真正需要什么?
  • 我想保留验证规则“密码”,但我不希望该插件自动验证 type='password' 的所有输入
  • 我使用来自bassistance.de/jquery-plugins/jquery-plugin-validation的jquery.validate.js
  • 我不明白你的意思。你想如何验证input[type="password"](因为你想离开password规则),但不验证这些输入?当您不想验证规则时,为什么需要规则 password

标签: jquery validation jquery-validate


【解决方案1】:

查看ignore option。您可以为其提供任何选择器,因此可能对您有用的是:

$('form').validate({
    ignore:'input[type="password"]'
});

如果这太笼统了,我建议在所有适当的输入中添加一个诸如ignoreValidation 的类,然后在ignore 选项中使用'.ignoreValidation'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2014-08-18
    • 2020-02-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-28
    相关资源
    最近更新 更多