0

I have a requirement by which need to check validation between number of days entered between two date selectors [From & To Dates]. My requirement is that it should not exceed 100 days.

Is there a way I can do with asp.net provided validators. I can go ahead and write customvalidator for it (both client and server side), but wondering if that is doable using CompareValidator or RangeValidator?

DotNetInfo
  • 3,344
  • 6
  • 33
  • 39

2 Answers2

1

Try using custom validator:

  <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>   

Call the following function in 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;
     }

  }

Hope this will do it for you...

Harun
  • 5,109
  • 4
  • 38
  • 60
  • Thanks man. Yeah I always had that option on hand but wanted to know if anyone had any smart implementation using Compare or Range Validator. I knew wasn't feasible but there are some real smarts out there :) – DotNetInfo May 13 '11 at 02:08
0

Below function will do the work if you are looking after similar kinda answer

        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;
    }

Got help from somewhat similar post

Community
  • 1
  • 1
DotNetInfo
  • 3,344
  • 6
  • 33
  • 39