[英]jquery validation. custom rules using selector syntax
我正在編寫一個使用jQuery驗證的系統,該系統通過jSON引入自定義規則,消息並動態驗證單個頁面中的多種形式。 除了我找不到關於自定義規則的正確語法的任何體面的信息外,一切都在進行。
例如:
我知道這可行...
"ui_txt_GCPostcode": {
"required": "input[name=ui_rb_ItemAddress][value=Recipient]:checked"
}
我在這里說的是,僅ui_txt_GCPostcode
中名稱為ui_rb_ItemAddress
且值為Recipient
的列表中的單選按鈕時, ui_txt_GCPostcode
需要ui_rb_ItemAddress
。
在我看來,我能夠根據包含特定選擇器屬性的依賴項表達式來分配規則。
但是,這不起作用.....
"ui_sel_PCSelect": {
"required": "input[name=ui_txt_PostCodeSearch]:hidden, input[name=ui_txt_Address]:hidden"
}
即使我看到ui_txt_Address
,驗證器也會觸發。
我什至在設置帶有忽略隱藏屬性的驗證器。
// 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"
});
有任何想法嗎?? 我的期限很緊迫,我開始感到恐慌。
該選擇器:
"input[name=ui_txt_PostCodeSearch]:hidden, input[name=ui_txt_Address]:hidden"
如果第一部分或第二部分為true,則將觸發required
規則(請參見多重選擇器 )。 這就是即使該規則可見也將觸發該規則的原因。
如果你想,如果兩者都需要隱藏的元素,你可能必須提供相關性-函數,而不是表達式:
"ui_txt_GCPostcode": {
"required": function() {
return $("input[name='ui_rb_ItemAddress'][value='Recipient']:checked").length &&
$("input[name='ui_rb_ItemAddress'][value='Recipient']:checked").length;
}
}
至於ignore
屬性不起作用,那是為了忽略與選擇器匹配的字段(因此:hidden
將告訴驗證器不要驗證隱藏字段)。 不知道您是否以這種方式使用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.