ASP.Net验证到达时间和天数之间的天数出发日

时间:2011-05-12 04:48:32

标签: javascript jquery .net asp.net validation

我有一个要求,需要检查两个日期选择器之间输入的天数之间的验证[来自&到日期]。我的要求是它不应超过100天。

我有办法用asp.net提供的验证器吗?我可以继续为它编写customvalidator(客户端和服务器端),但是想知道使用CompareValidator还是RangeValidator是否可行?

2 个答案:

答案 0 :(得分:1)

尝试使用自定义验证器:

  <asp:CustomValidator ID="valCustmCheckDate" runat="server" ErrorMessage="The date   difference should not be greater than 100 days" ForeColor="Red" ValidationGroup="LoginUserAdd" ClientValidationFunction="CompareStartAndEndDate"></asp:CustomValidator>   

在javascript中调用以下函数:

 function CompareStartAndEndDate(sender,args) {
    var txtFromExpiryDate = document.getElementById('<%=txtFromDate.ClientID %>');//dd/mm/yyyy format
    var txtToExpiryDate = document.getElementById('<%=txtToDate.ClientID %>');//dd/mm/yyyy format

     var a = txtFromDate.value.split('/');
     var b = txtToDate.value.split('/');

     var FromDate = new Date(a[2], a[1] - 1, a[0]);
     var ToDate = new Date(b[2], b[1] - 1, b[0]);

      var newFromDate =FromDate.getTime();
      var newToDate=ToDate.getTime();

      var dateDiffInMilliseconds= newToDate-newFromDate;

     var dateDiffInDays=dateDiffInMilliseconds/(1000 * 60 * 60 * 24)       


    if (dateDiffInDays>100 ) {
            args.IsValid = false;
     }
     else {
             args.IsValid = true;
     }

  }

希望这会为你做到......

答案 1 :(得分:0)

如果您正在寻找类似的答案

,下面的功能将完成工作
        function CheckDateRange(start, end, numberOfDays) {

        // Parse the entries
        var startDate = Date.parse(start);
        var endDate = Date.parse(end);
        // Make sure they are valid
        if (isNaN(startDate)) {
            alert("The start date provided is not valid, please enter a valid date.");
            return false;
        }
        if (isNaN(endDate)) {
            alert("The end date provided is not valid, please enter a valid date.");
            return false;
        }
        // Check the date range, 86400000 is the number of milliseconds in one day
        var difference = (endDate - startDate) / (86400000 * numberOfDays);
        if (difference < 0) {
            alert("The start date must come before the end date.");
            return false;
        }
        if (difference >= 1) {
            alert("The range must not exceed 100 days.");
            return false;
        }
        return true;
    }

从某些类似的post

获得帮助