4

Goal: I have an 'EndDate' TextBox that updates upon the user changing it. I want to be able to check/validate the date in EndDateTextBox.Text and make sure it is less than today's date (in format ex: 4/19/2013).

I've tried two methods:

Method One

<asp:TextBox ID="HiddenTodayDate" Visible = "false" runat="server" />
<asp:CompareValidator ID="CompareEndTodayValidator" Operator="GreaterThan" Type="Date" 
     ControlToValidate="HiddenTodayDate" ControlToCompare="EndDateTextBox" 
     ErrorMessage="'End Date' must be before today's date" runat="server" />

And the following in my Page_Load method:

HiddenTodayDate.Text = DateTime.Today.ToShortDateString();

Method Two

<asp:HiddenField ID="HiddenTodayDate" runat="server" />
<asp:CompareValidator ID="CompareEndTodayValidator" Operator="GreaterThan" Type="Date" 
     ControlToValidate="HiddenTodayDate" ControlToCompare="EndDateTextBox" 
     ErrorMessage="'End Date' must be before today's date" runat="server" />

And the following in my Page_Load method:

HiddenTodayDate.Value = DateTime.Today.ToShortDateString();

To the code savvy obviously setting a TextBox visible to false prevents the Validator from seeing it as well, but I didn't know it at the time and wanted to document my process. When I try method two, I come across the following error when debugging:

Control HiddenTodayDate referenced by the ControlToValidate property of `CompareEndTodayValidator cannot be validated.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Control 'HiddenTodayDate' referenced by the ControlToValidate property of 'CompareEndTodayValidator' cannot be validated.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

So is there a means to somehow achieve my goal without having to display DateTime.Today somewhere? I'd prefer to keep my code as simple and clean as possible and not use Javascript, but if Javascript provides the only workaround, then so be it. Code would be greatly appreciated. Thanks!

Kurt Wagner
  • 3,295
  • 13
  • 44
  • 71

2 Answers2

9

After learning of the the ValueToCompare property due in part to Tim's post I was able to search around and find a question similar to mine with an answer that almost worked (had to change ASP.NET compare type to String): Using the CompareValidator control to compare user input date with today's date

Here's what my code looks like:

ASP.NET:

<asp:CompareValidator ID="CompareEndTodayValidator" Operator="LessThan" type="String" ControltoValidate="EndDateTextBox" ErrorMessage="The 'End Date' must be before today" runat="server" />

.NET:

(In Page_Load method)

CompareEndTodayValidator.ValueToCompare = DateTime.Now.ToShortDateString();
Community
  • 1
  • 1
Kurt Wagner
  • 3,295
  • 13
  • 44
  • 71
4

You can set the ValueToCompare property programmatically to today:

<asp:comparevalidator runat="server" Id="CompareEndTodayValidator"
  errormessage="The date must be less than today"
  controltovalidate="EndDate" type="Date" Operator="LessThan"
  ValueToCompare="<%= DateTime.Today.ToShortDateString() %>" />

(not tested, if <%= doesn't work use <%#, then you have to remember to call Page.DataBind() somewhere(f.e. in Page_Load) if it is not in a databound context)

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • I've tried: `` But it results in a error: The value '<%= DateTime.Today.ToShortDateString() %>' of the ValueToCompare property of 'CompareEndTodayValidator' cannot be converted to type 'Date'. Using %# results in several build errors, most notably such as ), ; expected. – Kurt Wagner Apr 19 '13 at 23:36
  • Answer found thanks to you. thanks for such a quick response! – Kurt Wagner Apr 19 '13 at 23:47
  • @KurtWagner: What was the answer then? I thought you got an exception? Then you could try to change the culture (if possible) to en-US like here: http://stackoverflow.com/a/15547187/284240 – Tim Schmelter Apr 19 '13 at 23:49
  • The answer was this: http://stackoverflow.com/questions/2264215/using-the-comparevalidator-control-to-compare-user-input-date-with-todays-date (Specifically, Abhishek kumar's) I had to get the Value to Compare property in the Page_Load method and do one or two other things (i.e. use String type rather than Date) – Kurt Wagner Apr 22 '13 at 16:12