date Compare Validator issue in asp.net

3.1k views Asked by At

I have a compare validator which validates on two <telerik:RadDatePicker> Start date and End date. The validation rule is simple, Check if Start Date is greater than the End Date and show the error message to the user to correct it

it works as expected but when the start and end dates are the same it is showing the message which is not expected. Code Below:

<asp:CompareValidator ID="dateCompareValidator" runat="server" ControlToValidate="endDate" ControlToCompare="startDate" Operator="GreaterThan" Type="Date" ErrorMessage="Start Date is greater than the End Date - please correct dates."Display="Dynamic"></asp:CompareValidator>

and the date pickers are as follows for both start date and end date:

<telerik:RadDatePicker CssClass="rcCalPopup" ID="endDate" runat="server" 
                                Skin="Vista">
                                <DateInput ID="DateInput2" runat="server" LabelCssClass="radLabelCss_Vista" Skin="Vista">
                                </DateInput>
                                <Calendar ShowRowHeaders="false" ID="Calendar2" runat="server" UseRowHeadersAsSelectors="False" UseColumnHeadersAsSelectors="False"
                                    ViewSelectorText="x" Skin="Vista">
                                </Calendar>
                                <DatePopupButton CssClass="rcCalPopup"></DatePopupButton>
                            </telerik:RadDatePicker>
1

There are 1 answers

1
Tim Schmelter On BEST ANSWER

The rule you have defined with the Validator is:

  • Enddate must be greater than Startdate (note the missing equal)

The validator Operator property determines the rule for valid input, not invalid input.

So if you want to allow equal dates you have to use GreaterThanEqual

<asp:CompareValidator ID="dateCompareValidator" runat="server" 
    ControlToValidate="endDate" ControlToCompare="startDate" 
    Operator="GreaterThanEqual" Type="Date" 
    ErrorMessage="End date must be equal or greater than start date - please correct dates."Display="Dynamic">
</asp:CompareValidator>

Note that i've also changed the ErrorMessage accordingly. If the input control is empty, no validation functions are called and validation succeeds. Use a RequiredFieldValidator control to require the user to enter data in the input control.