tracer tong
tracer tong

Reputation: 553

c#.net using asp:CompareValidator with dates

I have dates generated by a Javascript enhanced textbox in the format dd/mm/yyyy, which when validated with a asp:comparevalidator fails to to validate correctly.

Here is the relevant code:

<asp:CompareValidator ID="CompareValidator4" runat="server" 
                      ControlToValidate="txtEndDate" ValueToCompare="txtStartDate" 
                      Display="None" 
                      ErrorMessage="End Date should be greater than or equal to Start Date." 
                      Type="Date" Operator="GreaterThanEqual" SetFocusOnError="True">    
</asp:CompareValidator>

<strong>Start Date</strong><asp:TextBox ID="txtStartDate" runat="server" Width="215px" CssClass="textfield" Style="width: 176px; margin-left:5px;"></asp:TextBox>&nbsp;&nbsp;

<strong>End Date</strong><asp:TextBox ID="txtEndDate" runat="server" Width="215px" CssClass="textfield" Style="width: 176px;  margin-left:5px;"></asp:TextBox>&nbsp;&nbsp;

In case it isn't clear I want the date in txtStartDate to be earlier than the one in txtEndDate.

When validated, I get gibberish results with no obvious pattern to when the validation fails. Can anyone see what is wrong?

btw I am aware of how poor the html is - I am editing someone else's code.

Upvotes: 0

Views: 1129

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460048

Can you change the page's culture to en-GB?

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 
Inherits="_Default" Culture = "en-GB" %>

Found here.

Update You have a bug in your validator markup. If you want to compare both textbox-dates with each other you have to provide the ControlToValidate and the ControlToCompare (not the ValueToCompare)

 <asp:CompareValidator ID="CompareValidator4" runat="server" 
    ControlToValidate="txtEndDate" 
    ControlToCompare="txtStartDate" 
    Type="Date" Operator="GreaterThanEqual"
    Display="None" ErrorMessage="End Date should be greater than or equal to Start Date." SetFocusOnError="True">
</asp:CompareValidator>

Upvotes: 1

Related Questions