views:

35

answers:

1

i have 2 textboxes that i am filling with StartDate, and Endate.
i want to use the compareValidator to make sure that the StartDate is less that the EndDate. i used this:

 <asp:CompareValidator ID="Comp" ControlToValidate="txtStartDate" ControlToCompare="txtEndDate" Operator="LessThan" Type="Date" runat="server" Display="dynamic" Text="Failed!" />

the validator is always giving me Failed no matter what the dates are. if startdate>endate or vise versa, Failed appears.
i am using (MM/dd/yyyy) format in the textboxes.

EDIT: ok i solved the problem. if anyone interested here is the new comparevalidator:

 <asp:CompareValidator ID="Comp" ControlToValidate="txtEndDate" ControlToCompare="txtStartDate" Operator="GreaterThan" Type="Date" runat="server" Display="dynamic" Text="Failed!" />
+1  A: 

The trick here is the ControlToValidate and ControlToCompare.

in your case the ControlToValidate should be the textbox that has the start date and ControlToCompare should have bthe textbox that has the end date. sometimes people confuse them.

i used this code to check

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:CompareValidator ID="CompareValidator1" runat="server" 
            ErrorMessage="start date should be less than end date" ControlToCompare="TextBox2" 
            ControlToValidate="TextBox1" Display="Dynamic" Operator="LessThan" Type="Date"></asp:CompareValidator>

i hope it helps

Pankaj Kumar