0

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.

0

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Hi. I followed your link and tryed a few things, in the process of which I found this line: CompareValidator4.ValueToCompare = DateTime.Now.ToShortDateString(); in the page_load function which doesn't appear to get updated when the controls are validated. Any idea how to correct this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.