compareValidator Compare to textboxes with dates

CPM picture CPM · Mar 5, 2012 · Viewed 8.2k times · Source

My compare validator is firing always even if it is greater and even if it is less.

<tr>
    <td>
        Selection Start Date:
    </td>
    <td>
        <asp:TextBox ID="SelectionStartDateTextBox" runat="server" 
                    Text='<%# Bind("SelectionStartDate") %>'></asp:TextBox>
        <asp:RequiredFieldValidator runat="server" ID="RequFilesStart" 
                    ControlToValidate="SelectionStartDateTextBox"
            ErrorMessage="Enter Selection Start date!" ValidationGroup="validation1">
        </asp:RequiredFieldValidator>
    </td>
</tr>
<tr>
    <td>
        Selection End Date:
    </td>
    <td>
        <asp:TextBox ID="SelectionEndDateTextBox" runat="server" 
                   Text='<%# Bind("SelectionEndDate") %>'></asp:TextBox>
        <asp:RequiredFieldValidator runat="server" ID="ReqFiledEnd" 
                    ControlToValidate="SelectionEndDateTextBox"
            ErrorMessage="Enter Selection End date!" ValidationGroup="validation1">
        </asp:RequiredFieldValidator>
        <asp:CompareValidator ID="CompareValSelDate" runat="server" 
            ControlToValidate="SelectionEndDateTextBox"
            ControlToCompare="SelectionStartDateTextBox" 
            CultureInvariantValues="true" Type="Date"
            Operator="LessThanEqual" ValidationGroup="validation1" 
            ErrorMessage="Selection End Date should be greater then Selected start date"
            Display="dynamic">        

        </asp:CompareValidator>
    </td>
</tr>

I also have set it up Culture="en-GB" and have also set it up same on my web config. I am using Ajax toolkit to display calendar attached to the TextBox

<cc1:calendarextender id="Calendarextendera3" runat="server" format="dd MMM yyyy"
            targetcontrolid="SelectionEndDateTextBox">
</cc1:calendarextender>

<cc1:calendarextender id="Calendarextendera4" runat="server" format="dd MMM yyyy"
            targetcontrolid="SelectionStartDateTextBox">
</cc1:calendarextender>

Please help I dont know what to do.

Thanks in advance

Answer

AGuyCalledGerald picture AGuyCalledGerald · Mar 5, 2012

You should set

CultureInvariantValues="false" 

in your CompareValidator. Too, the date format should be that of your current culture. The CompareValidator seems not to be able to compare the format dd MMM yyyy. If you want to display this format, you can use a custom validator to compare the dates:

  <asp:CustomValidator runat="server" ID="datesValidator" OnServerValidate="DatesValidator_Validate" ErrorMessage="end date should be greater than  or equal to start date"></asp:CustomValidator>

protected void DatesValidator_Validate(object source, ServerValidateEventArgs args)
{
    DateTime startDate = Convert.ToDateTime(SelectionStartDateTextBox.Text);
    DateTime endDate = Convert.ToDateTime(SelectionEndDateTextBox.Text);

    if (endDate < startDate)
    {
        args.IsValid = false;
    }
}