-
Validate a Date
I have three drop down list boxes that allow the user to provide a date and time that will be added to a database. I want to assure that the date and time occur after the present date and time.
I tried using a custom validator, and the error message does appear if the date selected is not after the current date, but for some reason it still allows the information to be processed into the database.
Any idea what I am doing wrong here? Why does the information get processed even when I am setting the isvalid property to false?
private void rfDate_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{
DateTime dt = System.DateTime.Now;
DateTime re = new DateTime(Convert.ToInt32(ddYear.SelectedItem.ToString()),ddMonth.SelectedIndex+1,ddDay.SelectedIndex +1,ddHour.SelectedIndex+1,0,0,0);
//determine if the date selected is greater than right now
if (re > dt)
args.IsValid = true;
else
args.IsValid = false;
}
-
I'm not positive on how to do this in C# (or C++), but when I use the custom validator in VB.NET I have to also put client side validation in the HTML portion of the code.
It looks something like your code:
Code:
if (re > dt)
args.IsValid = true;
else
args.IsValid = false;
Anyway, I hope this is somewhat helpful to you.