I ran into a frustrating error recently, where my compare validator was refusing to co-operate when testing 2 dates.
Here is my code:
Start date:
<asp:TextBox runat="server" ID="txtStartDate" Width="80px" CssClass="TextField"
Text='<%# Bind("startdate","{0:d}") %>' />
End date:
<asp:TextBox runat="server" ID="txtenddate" Width="80px" CssClass="TextField"
Text='<%# Bind("enddate","{0:d}") %>' />
<asp:CompareValidator ID="CompareValidator1" ControlToCompare="txtstartdate"
ControlToValidate="txtenddate"
Operator="GreaterThanEqual" ErrorMessage="The start date has to be before the end date"
Display="None" runat="server" />
The problem was that the Compare Validator would not work when the start date was 05/06/2008 and the end date was 03/07/2008 (these are all in DD/MM/YYYY). The reason was that the compare validator did not realise the value was a date and saw simply that 05 was greater than 03 in terms of ASCII values.
The fix should have been simply to add type="date" to the CompareValidator, however on it's own that doesn't work if you're using UK English dates - you also need to add CultureInvariantValues="true" (unless your Globalisation is set properly in your web.config - which mine wasn't...)
The full code then becomes:
Start date:
<asp:TextBox runat="server" ID="txtStartDate" Width="80px" CssClass="TextField"
Text='<%# Bind("startdate","{0:d}") %>' />
End date:
<asp:TextBox runat="server" ID="txtenddate" Width="80px" CssClass="TextField"
Text='<%# Bind("enddate","{0:d}") %>' />
<asp:CompareValidator ID="CompareValidator2" ControlToCompare="txtstartdate"
ControlToValidate="txtenddate" CultureInvariantValues="true" Type="Date"
Operator="GreaterThanEqual" ErrorMessage="The start date has to be before the end date"
Display="None" runat="server" />
MSDN has more information here: http://msdn.microsoft.com/en-us/library/a7zyyk0c(VS.80).aspx
Bartek Marnane