2012-12-15 160 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()