I have a text box on a web form where a user will input a Discharge Date...The date MUST be entered in this format MM/DD/YYYY what C# code would validate that the entry is input in that format, and if not show an error? Something like:
ButtonFinish_Click()
{
//check to see if the date is correct format
try
{
//how to check?
//if valid Response.Redirect -- Go to next page
}
catch
{
Error.Text = "Dates must be entered in a MM/DD/YYYY Format. Please check the entry and try again."
Loading

VulpesRank #55 · 29.2K reputationPosted Feb 4, 2013, 7:08 AM EST
else
{
// it's not OK
}
richard smithRank #8,268 · 225 reputationPosted Feb 4, 2013, 7:22 AM EST
The best overloaded method match for System.DateTime.TryParseExact(string, string, SystemIFormatProvider, System.Globalization.DateTimeStyles, out System.DateTime) has some invalid arguments.
Satyapriya NayakRank #47 · 31.5K reputationPosted Feb 4, 2013, 7:25 AM EST
http://paulschreiber.com/blog/2007/03/02/javascript-date-validation/
richard smithRank #8,268 · 225 reputationPosted Feb 4, 2013, 7:29 AM EST
I am assuming that is javascript, and I need it to be C#
VulpesRank #55 · 29.2K reputationPosted Feb 4, 2013, 7:52 AM EST
Note in particular that 'dt' should be preceded by 'out' as it's an output parameter.
You can replace '0' with DateTimeStyles.None if you want to be more verbose.
Suthish NairRank #92 · 22.8K reputationPosted Feb 4, 2013, 9:07 AM EST
A small change... But why just use a date control.
DateTime dt1;
VulpesRank #55 · 29.2K reputationPosted Feb 5, 2013, 1:11 AM EST
So, with a simple date format such as MM/dd/yyyy, it doesn't matter whether you use that or the Invariant culture.