jQuery验证器 - 同一控件的不同错误消息

时间:2011-06-01 09:47:47

标签: jquery-ui jquery-validate

在我的MVC验证中,我使用日历控件,所有验证都可以正常工作。

var today = $('#TodayDateHf').val();

$('#myform').validate({

    errorPlacement: $.calendars.picker.errorPlacement,

    rules: {

        DateFrom: {
            cpDate:true,
            cpCompareDate: { notAfter: '#DateTo', 'notAfter' : today } 
        },
        DateTo: {
            cpDate:true,
            cpCompareDate: { notBefore: '#DateFrom' } 
        }     

    },

    messages: { 
        DateFrom: 'Please enter a valid date less than or equal to To date',
        DateTo: 'Please enter a valid date greater than or equal to From date'

    }

});

一切正常,但我的问题是我不知道如何为同一控件显示不同的消息。

例如,对于DateFrom,定义了规则,该规则中有两个参数,但它只显示相同的错误消息。如何根据评估的参数显示不同的错误消息?

1 个答案:

答案 0 :(得分:2)

你可以使用这样的东西

messages:{
                DateFrom:{
                    cpDate:"Message1",
                    cpCompareDate: "Message2"
                }

            }
相关问题