I am finishing a form in C#, and I would like to validate the data entry. I have seen numerous posts about validating with "CompareValidator". That's fine and clear.
I just would like to ask about a twist: I have the date in one field, result of using the AjaxControlToolkit Calendar Extender. You can retrieve it as:
DateField.Text;
Then I have the hour in another field of the form, using MKB Time Select package from NuGet.
TimeSelector1.Hour + ":" + TimeSelector1.Minute
is the value I want to use.
So the user has been filling the form, and picked a date from the date field, and an hour from the time selector. I want to check if the date and time that the user selects in these fields, is at least 24 hours greater than the current time, so it means the validator needs to use these two different fields to grab the initial data.
I am a bit confused about how would I get started about that.
Basically, the validator should show the warning if
DateField.Text + " " + TimeSelector1.Hour + ":" + TimeSelector1.Minute
is greater than
DateTime.Now.AddHours(24)
But I don't see clear how to tell the validator to validate this sort of statements, rather than the plain content on the fields.
With the information provided, you should be able to parse your values into a DateTime object.
string dateString = String.Format("{0} {1}:{2}:00", DateField.Text, TimeSelector1.Hour, TimeSelector1.Minute);
DateTime selectedDateTime = new DateTime();
if (DateTime.TryParse(dateString, out selectedDateTime))
{
if (selectedDateTime > DateTime.Now.AddHours(24))
{
// code
}
}