Search code examples
asp.netajaxvalidationcalendarextender

problem in comparing two dates


i use two CalendarExtender to combine two dates: start date and end date and i use a compare validator to validate that the end date is greater than the start date.

the problem that the validator fires when the day in the end date is smaller than the day in the start date even though the whole end date is greater than the whole start date ..

how to fix this problem?

my aspx:

1-start date:

<asp:TextBox ID="txt_startDate" runat="server" ValidationGroup="insertgroup" MaxLength="10"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txt_startDate"
    ErrorMessage="!" ValidationGroup="insertgroup"></asp:RequiredFieldValidator>
<cc1:MaskedEditExtender ID="txt_startDate_MaskedEditExtender" runat="server" CultureAMPMPlaceholder=""
    CultureCurrencySymbolPlaceholder="" CultureDateFormat="" CultureDatePlaceholder=""
    CultureDecimalPlaceholder="" CultureThousandsPlaceholder="" CultureTimePlaceholder=""
    Enabled="True" Mask="99/99/9999" MaskType="Date" TargetControlID="txt_startDate">
</cc1:MaskedEditExtender>
<cc1:CalendarExtender ID="txt_startDate_CalendarExtender" runat="server" Enabled="True"
    TargetControlID="txt_startDate" Format="dd/MM/yyyy">
</cc1:CalendarExtender>

2-end date:

<asp:TextBox ID="txt_endDate" runat="server" ValidationGroup="insertgroup" MaxLength="10"></asp:TextBox>
<cc1:CalendarExtender ID="txt_endDate_CalendarExtender" runat="server" TargetControlID="txt_endDate"
    Format="dd/MM/yyyy">
</cc1:CalendarExtender>
<cc1:MaskedEditExtender ID="txt_endDate_MaskedEditExtender" runat="server" CultureAMPMPlaceholder=""
    CultureCurrencySymbolPlaceholder="" CultureDateFormat="" CultureDatePlaceholder=""
    CultureDecimalPlaceholder="" CultureThousandsPlaceholder="" CultureTimePlaceholder=""
    Enabled="True" Mask="99/99/9999" MaskType="Date" TargetControlID="txt_endDate">
</cc1:MaskedEditExtender>
<asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" ControlToValidate="txt_endDate"
    Display="Dynamic" ErrorMessage="!" ValidationGroup="insertgroup"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator2" runat="server" ControlToCompare="txt_startDate"
    ControlToValidate="txt_endDate" Display="Dynamic" ErrorMessage="????? ??????? ??? ?? ???? ??? ?? ????? ???????"
    Operator="GreaterThan" Type="Date" ValidationGroup="insertgroup"></asp:CompareValidator>

EDIT: Example for two dates make problems:

//error message
    start date: 
    28/01/2014

    end date :
    25/07/2014

while

// no error message
    start date: 
    28/01/2014

    end date :
    01/07/2014

Solution

  • Here's some more information as to why you're getting this behavior, and why Chad's code will help you.

    Right now your application is running under a culture setting that says dates should be mm/dd/yyyy (your computer or the webserver is working like an American). If you look at the javascript that ASP.NET emits for the Validators, you'll see that when doing a comparison it first checks whether the datatype of the ControlToValidate is correct. It does this by parsing the entered value with a RegEx, and expects the month value to be first and then the day (because of the culture setting). When it sees your value of 25, it doesn't think this is a valid month, and hence says that your date is not valid. It then returns false, without even checking the second field's date value or trying to compare them.

    In your second example, validation passes because first, it checks the datatype of the end date, which CAN be a valid mm/dd/yyyy date. Then it checks the datatype of the second date, which because of the 28, causes it to think it's not a valid date. The validator then returns true, because it assumes that a valid date is greater than a non-valid date.

    Chad's code will set the culture of your page's thread to one that uses the dd/MM/yyyy format, and therefore your validators will use that when checking whether your dates are valid.

    Here's the MSDN page with more info about the culture settings and what they do.