如何在jquery Validate错误消息中包含字段标签

时间:2011-02-19 23:08:33

标签: jquery jquery-validate

我在页面顶部显示我的jquery验证错误。我想在每条消息旁边包含与每个无效字段关联的标签的文本值。怎么做这是我的jquery。

$(document).ready(function(){
    $("#reqAccount").validate({
            errorClass: "error-text",
            validClass: "valid",
            errorLabelContainer: "#errorList",
            wrapper: "li ",
            highlight: function(element, errorClass, validClass) {
                $(element).addClass("error-input").addClass(errorClass).removeClass(validClass);
             },
             unhighlight: function(element, errorClass, validClass) {
                $(element).removeClass("error-input").removeClass(errorClass).addClass(validClass);
             }
        });
    });

4 个答案:

答案 0 :(得分:3)

如果我理解正确,我认为您可以使用showErrors

$(".selector").validate({
   showErrors: function(errorMap, errorList) {
      //use the fn params to get a map of current errors,
      //then append to your <li> elements
   }
});

参考。 {选项'标签下的http://docs.jquery.com/Plugins/Validation/validate

答案 1 :(得分:3)

以下是我最终更新消息以包含标签的文本值的方法。文本字段的id在errorMap中找到,因此我使用它们来查找具有类似ID的标签并将它们附加到errorList。如果有更好的方法,请评论。

  $(document).ready(function(){
    $("#reqAccount").validate({
        errorClass: "error-text",
        validClass: "valid",
        errorLabelContainer: "#errorList",
        wrapper: "li class='indent error-text'",
        showErrors: function(errorMap, errorList) {
            var i = 0;
            var labelText = new Array(this.numberOfInvalids());
            $.each(errorMap, function(name, value) {
                labelText[i] = $("#" + name + "Label").text();
                i++;
            });
            i = 0;
            $.each(errorList, function(name, value) {
                this.message = labelText[i] + " " + this.message;
                i++;
        });
            this.defaultShowErrors();

         },

        highlight: function(element, errorClass, validClass) {
            $(element).addClass("error-input").addClass(errorClass).removeClass(validClass);
         },
         unhighlight: function(element, errorClass, validClass) {
            $(element).removeClass("error-input").removeClass(errorClass).addClass(validClass);
         }
    });
});

答案 2 :(得分:2)

编辑感谢您发布的答案,您发布的showErrors代码帮助了我很多。但是,当onfocusout设置为true时,我无法让它为我工作。我不得不稍微修改你的代码,希望这能帮助那些处理同样问题的人。

showErrors: function(errorMap, errorList) {
                var i = 0;
                var labelText = new Array(this.numberOfInvalids());

                $.each(errorMap, function(name, value) {
                    //I had to change the following line to get the for attribute of the 
                    //label that matches the id of the name
                    var label = $("label[for='" + $('#' + name).attr('id') + "']").text();
                    labelText[i] = label;
                    i++;
                 });
                i = 0;
                $.each(errorList, function(name, value) {
                    //This is where I ran into issues.  With the code you had earlier, the labelText kept
                    //getting appended every time I would change focus from an input.  To get rid of that
                    //I had to run a couple of checks
                    var semi = labelText[i].indexOf(':');
                    var requiredString = 'This field is required';
                    var check = labelText[i].indexOf(requiredString); 

                    if (check != -1) {
                        if (semi != -1 && labelText[i].indexOf(':', semi + 1) != -1) {
                            var indexOfSemi = labelText[i].indexOf(':'); 
                            labelText[i] = labelText[i].substr(0, indexOfSemi); console.log(labelText[i]);
                            this.message = hold + ": " + this.message;
                        }
                    } else {
                        this.message = labelText[i] + " " + this.message;d
                        i++;
                    }
                });
                this.defaultShowErrors();
            },

只是想表示感谢,希望这也可以帮助其他人。

答案 3 :(得分:1)

$.validator.messages.required = function (param, input) {
     return 'The ' + $("label[for='"+ input.name +"'].lblRequired").text()
     + ' field is required';
}

我使用它,它对我来说非常适合。当您使用jQuery选择相应的标签时,请务必为您的标签添加一个额外的标识符(在我的情况下,我添加了类“.lblRequired”)。当验证器触发时,它会创建具有相同“for”的新标签,并将它们与输入相关联。因此,如果您没有添加类或其他方式来识别原始标签,那么当您为后续验证选择错误的标签时,您将最终弄得一团糟,并且标签将继续与错误消息复合。