簡體   English   中英

jQuery.validate忽略密碼

[英]jQuery.validate ignore password

我使用jquery.validate.js來驗證我們網站上的表單。 今天,我將驗證插件的版本更新為1.9版,此后,我在網站上遇到密碼驗證問題。

我發現了為什么會發生這種情況,現在我正在尋找解決問題的“正確”方法。 在以前的版本中,方法attributeRules如下:

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;
        }

從中我了解到,在以前的版本中,該方法沒有檢查輸入元素的type屬性,並且沒有添加“密碼”驗證器。 在1.9版中,它檢查元素是否具有“ password”類型並添加驗證器。

問題:
如何告訴jQuery.validator忽略帶有“密碼”類型的輸入?

謝謝

簽出忽略選項 您可以為其提供任何選擇器,因此可能對您有用的是這樣的:

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

如果這太籠統,我建議向所有適當的輸入添加一個類,例如ignoreValidation ,然后在ignore選項中使用'.ignoreValidation'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM