2012-12-15 141 views
0

我在我的asp.net mvc页面上使用jquery daterange验证器http://docs.jquery.com/Plugins/Validation/multiplefields。我的HTML看起来像这样:jquery日期范围验证器不显示错误消息

<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小于数据,我只收到“创建日期”消息。我想显示消息“请指定正确的日期范围,开始日期必须是之前的最新”这是默认的消息在红色

+1

您能否请张小提琴? – closure

+0

@raghavv html不是纯粹的,它是使用asp.net mvc视图生成的,无法使用小提琴 – DotnetSparrow

+0

您仍然可以使用从asp生成的html并在jsfiddle上使用它 –

回答

0

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

ignoreTitle: true 
0

新日期($(“#fromDate”).val())返回一个字符串。您应该比较millisec值,而不是:新日期($(“#fromDate”)。val())。getTime()