gpt4 book ai didi

jquery - 如何使用 addClassRules 将消息添加到类

转载 作者:行者123 更新时间:2023-12-03 21:47:28 34 4
gpt4 key购买 nike

我正在使用jQuery Validate验证具有大量重复数据行的页面。由于空间有限,每一行都有自己的验证摘要。

我使用 addClassRules 将验证规则应用到页面,但摘要中的默认错误消息过于笼统(例如“Field is required、Field is required 等)。

示例:

jQuery.validator.addClassRules({
amount: {
required: true,
range: [0, 2000000]
},
comment: {
maxlength: 51
},
owner: {
notFirstOption: true
},
A: {
required: function (element) {
return getParentElement($(element), "tr").find(".colB input.B").val().length > 0;
},
digits: true
}});

您可以为每个验证类中的规则应用自定义消息吗?理想情况下,我追求的是这样的东西:

jQuery.validator.addClassMessages({
amount: {
required: "Amount is required",
range: "Amount must be between 0 and 2,000,000"
},
comment: {
maxlength: "Comment may be a maximum of 51 characters"
},
owner: {
notFirstOption: "Please select an owner"
},
A: {
required: "A is required if B is entered",
digits: "A must be numeric"
}});

预先感谢您提供的任何帮助!

最佳答案

在文档中,答案是为要调用的方法指定别名并添加自定义消息。

Check the reference在“重构规则”标题下:

// alias required to cRequired with new message
$.validator.addMethod("cRequired", $.validator.methods.required,
"Customer name required");
// alias minlength, too
$.validator.addMethod("cMinlength", $.validator.methods.minlength,
// leverage parameter replacement for minlength, {0} gets replaced with 2
$.format("Customer name must have at least {0} characters"));
// combine them both, including the parameter for minlength
$.validator.addClassRules("customer", { cRequired: true, cMinlength: 2 });

关于jquery - 如何使用 addClassRules 将消息添加到类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3247305/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com