Start Date - End date check using gap comparison

I need the end date to always be greater than the start date, I tried checking with CompareValidator.

The code is as follows:

I have a start date for the text field

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

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

Another end date for TextBox.

<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 check for missing the comparison field is skipped.

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

+3
source share
3 answers

You can just try it like this.

<asp:CompareValidator ID="cmpVal1" ControlToCompare="txtStartDate" 
         ControlToValidate="txtEndDate" Type="Date" Operator="GreaterThanEqual"   
         ErrorMessage="*Invalid Data" runat="server"></asp:CompareValidator>
+10
source

It is right. This 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 do not forget to write:

cmpVal1.Validate() 

in the case where the comparison occurs.

+3
source

() () JAVASCRIPT

It takes care of From date to date, both must be less than the current date (Currentdate). From the date should be less than today.

Text field

<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 the Script block in the chapter 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>
0
source

All Articles