开发者

jQuery Validate - custom message not working

I want to create a custom error message for the 'required' rule. It should be easy using the 'messages' parameter to the validate method. But I can't seem to figure out why my code is not working.

I forked the fiddle from this question into this new fiddle.

Can anybody spot the error?

PS. Same question as this one, except in my case it's not the typo.

EDIT: migrated fiddle code here. HTML:

<form action="get">
    <div><input type="text" name="part1" class="part"></div>
    <div><input type="text" name="part2" class="part"></div>
    <div><input type="text" name="part3" class="part"></div>
    <div><input type="text" name="part4" class="part"></div>
    <input type="submit" value="Submit" />
</form>

javascript:

jQuery.validator.addMethod("require_from_group", function(value, element, options) {
    var valid = $(options[1], element.form).filter(function() {
        return $(this).val();
    }).length >= opti开发者_如何学编程ons[0];

    if(!$(element).data('reval')) {
        var fields = $(options[1], element.form);
        fields.data('reval', true).valid();
        fields.data('reval', false);
    }
    return valid;
}, jQuery.format("Please fill out at least {0} of these fields."));

$("form").validate({
    rules: {
        part1: { required: true },
        part2: { require_from_group: [2,".part"] },
        part3: { require_from_group: [2,".part"] },
        part4: { require_from_group: [2,".part"] }
    },
    messages: {
        required: "THIS MESSAGE IS NOT WORKING"
    }
});


You can try like this. Add the code just below the rules section

messages:{
                field name:{
                    required:"Please enter a value"                     
                }

            }


You did not specify the field to associate with the message. Your code:

messages: {
    required: "THIS MESSAGE IS NOT WORKING"
}

Should be:

messages: {
    part1: "THIS MESSAGE IS NOT WORKING"
}

I forked a fiddle from your problem into this one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜