Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Validating a date
Message
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01050107
Message ID:
01050127
Views:
11
>How to I validate a date?
>
>The user might enter '5/20', from which I create a string of '05/20/05'.
>
>I then convert it to date using:
>
>
>DateTime dDate = Convert.ToDateTime(sDate);
>
>
>But if the user enter2 '55/75', my string would then be '55/75/05' and
>the conversion to date errors.
>
>I want to validate it before I try to convert.
>
>I'm using C#.
>
>Thanks

Kevin,
Like Hilmar said you could use:
try
{
  DateTime dDate = Convert.ToDateTime(sDate);
}
catch
{
  MessageBox.Show(sDate+" is an invalid date.");
}
You could also use the validate event of the textbox (I assume you use a textbox to enter the date), where you can create custom validation code character by character.

You can also use regular expression to force write a correct date. (I think regular expression should work well maybe not)

Also you could use the datetime picker an let the user click the date instead of typing it (typing is so 20th century <s>)

Where do you call the Conver.ToDateTime method from anyways?

Einar
Semper ubi sub ubi.
Previous
Reply
Map
View

Click here to load this message in the networking platform