3

I need my end date to be always greater than start date, I tried validation using CompareValidator.

Code is as follows:

I have a text box start date

<asp:TextBox ID="TxtStartDate"
             runat="server" />

<asp:CalendarExtender Enabled="True"
                      ID="TxtStartDate_CalendarExtender"
                      TargetControlID="TxtStartDate"
                      runat="server" />

Another TextBox End date.

<asp:TextBox ID="TxtEndDate"
             runat="server" />

<asp:CalendarExtender Enabled="True"
                      ID="TxtEndDate_CalendarExtender"
                      TargetControlID="TxtEndDate"
                      runat="server" />

<asp:CompareValidator ControlToCompare="TxtStartDate"
                      ControlToValidate="TxtEndDate"
                      Display="Dynamic"
                      ErrorMessage="CompareValidator"
                      ID="CompareValidator1"
                      Operator="GreaterThan"
                      Type="Date"
                      runat="server" />

But the compare field validator misfires.

For example when start date is 2/04/2012 and end date is 10/04/2012 it fires.

Albireo
  • 10,977
  • 13
  • 62
  • 96
3lokh
  • 891
  • 4
  • 17
  • 39
  • Hope this will help you http://stackoverflow.com/questions/9735836/asp-net-compare-validator-issue-while-specifying-date-format-in-calender-extende – shanish May 21 '12 at 12:10

3 Answers3

10

Simply you can try like this

<asp:CompareValidator ID="cmpVal1" ControlToCompare="txtStartDate" 
         ControlToValidate="txtEndDate" Type="Date" Operator="GreaterThanEqual"   
         ErrorMessage="*Invalid Data" runat="server"></asp:CompareValidator>
shanish
  • 1,964
  • 12
  • 35
  • 62
  • @Nikhil probably the [thread's Culture and UI Culture are not set](http://msdn.microsoft.com/en-us/library/bz9tc508.aspx) (or not set correctly). – Albireo May 22 '13 at 14:36
3

This one is correct.. It solved my problem.

<asp:CompareValidator ID="cmpVal1" ControlToCompare="txtStartDate" ControlToValidate="txtEndDate" Type="Date" Operator="GreaterThanEqual"  ErrorMessage="ToDate should be greater than FromDate" runat="server"></asp:CompareValidator>

And don't forget to write:

cmpVal1.Validate() 

on the event where comparison occurs.

Rohini
  • 31
  • 2
0

START DATE(FROM) AND END DATE (TO) VALIDATION USING JAVASCRIPT

It care about From date and To date both should be less then todays date(Currentdate). From date should be less then To date.

Text box

<asp:TextBox ID="txtFromDate" runat="server" onChange="javascript: txtFromDateChanged(this)"></asp:TextBox>
<asp:TextBox ID="txtToDate" runat="server" onChange="javascript: txtToDateChanged(this);" ></asp:TextBox>

java script(Put Script block in Head Section)

<script type="text/javascript">

        function txtToDateChanged(sender, args) {
            var date = new Date();
            var startDate = Date.parse(document.getElementById('<%= txtFromDate.ClientID %>').value);
            var endDate = Date.parse(document.getElementById('<%= txtToDate.ClientID %>').value);
            var timeDiff = endDate - startDate;
            var daysDiff = Math.floor(timeDiff / (1000 * 60 * 60 * 24));

            if (date > endDate) {
                document.getElementById('<%= txtToDate.ClientID %>').value = "";
                alert('*Select date greater than Today.');
            }
            if (daysDiff < 0) {
                document.getElementById('<%= txtToDate.ClientID %>').value = "";
                alert('*FromDate should be less than Todate');
            }

        }
        function txtFromDateChanged(sender, args) {
            var date = new Date();
            var startDate = Date.parse(document.getElementById('<%= txtFromDate.ClientID %>').value);
            if (date > startDate) {
                document.getElementById('<%= txtFromDate.ClientID %>').value="";
                alert('*Select date greater than Today.');
            }
        }
    </script>