jquery日期范围验证程序未显示错误消息

时间:2012-12-15 10:30:47

标签: javascript jquery asp.net-mvc

我在我的asp.net mvc页面http://docs.jquery.com/Plugins/Validation/multiplefields上使用jquery daterange验证器。我的HTML看起来像这样:

 <tr>
            <td align="left" valign="top">
                <label>
                    <input name="fromDate" type="text" class="flat requiredDateRange jq_watermark" id="fromDate"
                        style="width: 190px" title="Creation From Date" readonly="readonly" />
                </label>
            </td>
        </tr>
        <tr>
            <td align="left" valign="top">
                <label>
                    <input name="toDate" type="text" class="flat requiredDateRange jq_watermark" id="toDate"
                        style="width: 190px" title="Creation To Date" readonly="readonly" />
                </label>
            </td>
        </tr>
        <tr>
            <td colspan="3" align="left" valign="top">
                <label id="error" class="error" style="display: block;">
                </label>
            </td>
        </tr>

在页面顶部,我正在使用它:

$(document).ready(function(){
        $("#fromDate").datepicker({ dateFormat: 'dd MM yy', changeYear: true, changeMonth: true });
        $("#toDate").datepicker({ dateFormat: 'dd MM yy', changeYear: true, changeMonth: true });
});

jQuery(function () {

    jQuery("#frmSearch").validate({
        groups: {
            datesnotnull: "fromDate toDate",
            dateRange: "fromDate toDate"
        },
        errorPlacement: function (error) {
            jQuery("#frmSearch").find("#error").append('error');
           jQuery("#frmSearch").find("#error").show();

        }
    });

});

我的daterangevalidator文件如下所示:

// a custom method for validating the date range
$.validator.addMethod("datesnotnull", function () {  
    return (($("#fromDate").val().length != 0) && ($("#toDate").val().length != 0)) || (($("#fromDate").val().length == 0) && ($("#toDate").val().length == 0));
}, "Please specify the date range, from and to date.");

// a custom method for validating the date range
$.validator.addMethod("dateRange", function () {


    return ((($("#fromDate").val().length==0) && ($("#toDate").val().length==0)) || (new Date($("#fromDate").val()) < new Date($("#toDate").val())));
}, "Please specify a correct date range, the from date must be before the to date.");


// a new class rule to group all three methods
$.validator.addClassRules({
    requiredDateRange: { required: false, date: true, datesnotnull : true , dateRange: true  }    
});

// overwrite default messages
$.extend($.validator.messages, {    
    date: "Please specify valid dates"
});

但是当todate小于数据时,我只会收到“Creation To Date”消息。我想显示消息“请指定正确的日期范围,起始日期必须在日期之前”,这是红色的默认消息

2 个答案:

答案 0 :(得分:0)

您应该在validate()中使用以下选项:

ignoreTitle: true

答案 1 :(得分:0)

new Date($(“#fromDate”)。val())返回一个字符串。您应该比较millisec值:new Date($(“#fromDate”)。val())。getTime()