2012-06-23 92 views
1

我想對我的日期進行驗證,以便enddate比開始日期更大但不工作。我做錯了什麼?結束日期大於開始日期驗證javascript不能正常工作

<head> 
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />  
    <script src="@Url.Content("~/Scripts/jquery-ui-1.8.19.min.js")" type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/jquery-ui.unobtrusive.min.js")" type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/jquery-ui.unobtrusive-0.5.0.min.js")" type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/gen_validatorv4.js")" type="text/javascript"></script> 

    </head> 
    @using (Html.BeginForm("SearchFree", "Reservation", FormMethod.Get,new {id = "form" })) 
    {      

    <h7>Introduceti perioada Rezervarii</h7> 
<div class="editor-label"> 
<label id="cautare" for="StartDate">Data Intrare: </label>@(Html.JQueryUI().Datepicker("StartDate").DateFormat("mm-dd-yy").MinDate(DateTime.Today).ShowButtonPanel(true).ChangeYear(true).ChangeMonth(true).NumberOfMonths(2)) 
</div> 
<div class="editor-label"> 
<label id="cautare" for="EndDate">Data Iesire:</label>@(Html.JQueryUI().Datepicker("EndDate").DateFormat("mm-dd-yy").MinDate(DateTime.Today).ShowButtonPanel(true).ChangeYear(true).ChangeMonth(true).NumberOfMonths(2)) 

</div> 

<p> 
    <input id="buton1" type="submit" value="Cauta camere libere" /> 
</p> 

     } 
     <script type="text/javascript"> 
     $(document).ready(function() { 
    $.validator.addMethod("EndDate", function (value, element) { 
     var startDate = $('.StartDate').val(); 
     return Date.parse(startDate) <= Date.parse(value); 
    } 
     , "* End date must be after start date"); 
    $('.form').validate(); 
    }); 
     </script> 
+0

什麼是你得到的錯誤? –

+0

我沒有得到任何錯誤,只是沒有進行驗證 – jonny

+0

嘗試我發佈的代碼作爲答案,並讓我知道它是否適用於您...您當前的代碼可能會給你錯誤的地方。要查看錯誤,您可以使用Firebug調試jQuery代碼,例如:http://getfirebug.com/ –

回答

0

試試這個:

jQuery.validator.addMethod("greaterThan", 
function(value, element, params) { 

    if (!/Invalid|NaN/.test(new Date(value))) { 
     return new Date(value) > new Date($(params).val()); 
    } 

    return isNaN(value) && isNaN($(params).val()) 
     || (Number(value) > Number($(params).val())); 
},'Must be greater than {0}.'); 

使用這種方式:

$(".EndDate").rules('add', { greaterThan: ".StartDate" }); 

$("form").validate({ 
    rules: { 
     EndDate: { greaterThan: ".StartDate" } 
    } 
}); 
相關問題