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

前端之家收集整理的这篇文章主要介绍了jquery – 如何使用addClassRules将消息添加到类中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用 jQuery Validate来验证具有大量重复数据行的页面.由于空间有限,每行都有自己的验证摘要.

我使用addClassRules将验证规则应用于页面,但是在摘要中默认错误消息太泛泛(例如“需要字段,需要字段等等).

例:

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 });
原文链接:https://www.f2er.com/jquery/180365.html

猜你在找的jQuery相关文章