【问题标题】:how to add error message and custom image to error filed using jquery validation plugin如何使用 jquery 验证插件将错误消息和自定义图像添加到错误字段
【发布时间】:2011-11-11 01:26:30
【问题描述】:

我正在使用 jquery 验证插件(我很新),如果字段出错,我需要在标签旁边显示错误消息以及另一个自定义图像。

这是我目前拥有的:

messages: {
    fname: "Enter your firstname", 
        lname: "Enter your lastname"
},
errorPlacement: function(error, element) {
        error.appendTo( element.parent("td").next());

},

如何添加代码以将图像附加到文本中?

【问题讨论】:

    标签: jquery validation jquery-plugins


    【解决方案1】:

    这里是example

    以及这些示例的代码

    // extend the current rules with new groovy ones
    
        // this one requires the text "buga", we define a default message, too
        $.validator.addMethod("buga", function(value) {
            return value == "buga";
        }, 'Please enter "buga"!');
    
        // this one requires the value to be the same as the first parameter
        $.validator.methods.equal = function(value, element, param) {
            return value == param;
        };
    
        $().ready(function() {
            var validator = $("#texttests").bind("invalid-form.validate", function() {
                $("#summary").html("Your form contains " + validator.numberOfInvalids() + " errors, see details below.");
            }).validate({
                debug: true,
                errorElement: "em",
                errorContainer: $("#warning, #summary"),
                errorPlacement: function(error, element) {
                    error.appendTo( element.parent("td").next("td") );
                },
                success: function(label) {
                    label.text("ok!").addClass("success");
                },
                rules: {
                    number: {
                        required:true,
                        minlength:3,
                        maxlength:15,
                        number:true 
                    },
                    secret: "buga",
                    math: {
                        equal: 11   
                    }
                }
            });
    
        });
    

    还有一段 CSS

    em.error {
      background: url("images/unchecked.gif") no-repeat 0px 0px;
      padding-left: 16px;
    }
    

    【讨论】:

    • 在上面的示例中,图像与文本相结合。我需要一些东西,分别添加图像和文本。我需要文本显示在字段下方的框中,并且显示在字段标签旁边的图像。
    • 另外,当我在字段中输入正确的值时,我需要图像和文本消失
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    • 2012-05-07
    • 2011-10-22
    • 2017-01-18
    • 1970-01-01
    相关资源
    最近更新 更多