简体   繁体   English

jQuery验证。 使用选择器语法的自定义规则

[英]jquery validation. custom rules using selector syntax

I'm writing a system utilizing jQuery Validation that pulls in custom rules, messages via jSON and dynamically validates multiple forms in single pages. 我正在编写一个使用jQuery验证的系统,该系统通过jSON引入自定义规则,消息并动态验证单个页面中的多种形式。 All is going swimmingly except I cannot find any decent information on the correct syntax for custom rules.... 除了我找不到关于自定义规则的正确语法的任何体面的信息外,一切都在进行。

For example: 例如:

I know this works.... 我知道这可行...

"ui_txt_GCPostcode": {
    "required": "input[name=ui_rb_ItemAddress][value=Recipient]:checked"
}

I'm saying here that ui_txt_GCPostcode is required only when a radio button from a list with the name ui_rb_ItemAddress and the value Recipient is checked. 我在这里说的是,仅ui_txt_GCPostcode中名称为ui_rb_ItemAddress且值为Recipient的列表中的单选按钮时, ui_txt_GCPostcode需要ui_rb_ItemAddress

This to me looks like I am able to assign ruled based upon dependency expressions containing specific selector attributes. 在我看来,我能够根据包含特定选择器属性的依赖项表达式来分配规则。

This however doesn't work..... 但是,这不起作用.....

"ui_sel_PCSelect": {
        "required": "input[name=ui_txt_PostCodeSearch]:hidden, input[name=ui_txt_Address]:hidden"
    }

The validator is firing even though I have ui_txt_Address visible. 即使我看到ui_txt_Address ,验证器也会触发。

I'm even setting up the validator with the ignore hidden property eg. 我什至在设置带有忽略隐藏属性的验证器。

                // Validate the form once the defaults are set.
                $validator = $form.validate({
                    // This prevents validation from running on every
                    // form submission by default.
                    onsubmit: false,
                    messages: messages,
                    rules: rules,
                    errorContainer: $container,
                    errorLabelContainer: $("ol", $container),
                    wrapper: "li",

                    // Ignore hidden items. Why is this not working??
                    ignore: ":hidden"
                });

Any ideas?? 有任何想法吗?? I'm on a pretty tight deadline and I'm starting to panic. 我的期限很紧迫,我开始感到恐慌。

This selector: 该选择器:

"input[name=ui_txt_PostCodeSearch]:hidden, input[name=ui_txt_Address]:hidden"

Will trigger the required rule if either the first or second part are true (see the multiple selector ). 如果第一部分第二部分为true,则将触发required规则(请参见多重选择器 )。 This is why the rule fires even if one is visible. 这就是即使该规则可见也将触发该规则的原因。

If you want to make the element required if both are hidden, you may have to supply a dependency-function instead of an expression: 如果你想,如果两者都需要隐藏的元素,你可能必须提供相关性-函数,而不是表达式:

"ui_txt_GCPostcode": {
    "required": function() {
        return $("input[name='ui_rb_ItemAddress'][value='Recipient']:checked").length &&
            $("input[name='ui_rb_ItemAddress'][value='Recipient']:checked").length;
    }
}

As for the ignore property not working, that's for ignoring fields matching the selector (so :hidden will tell validator not to validate hidden fields). 至于ignore属性不起作用,那是为了忽略与选择器匹配的字段(因此:hidden将告诉验证器不要验证隐藏字段)。 Not sure if you were using it this way. 不知道您是否以这种方式使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM