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
VulpesPosted Feb 4, 2013, 12:08 PM
else
{
// it's not OK
}
VulpesPosted Feb 5, 2013, 6:11 AM
So, with a simple date format such as MM/dd/yyyy, it doesn't matter whether you use that or the Invariant culture.
Suthish NairPosted Feb 4, 2013, 2:07 PM
A small change... But why just use a date control.
DateTime dt1;
VulpesPosted Feb 4, 2013, 12:52 PM
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.
richard smithPosted Feb 4, 2013, 12:29 PM
I am assuming that is javascript, and I need it to be C#
Satyapriya NayakPosted Feb 4, 2013, 12:25 PM
http://paulschreiber.com/blog/2007/03/02/javascript-date-validation/
richard smithPosted Feb 4, 2013, 12:22 PM
The best overloaded method match for System.DateTime.TryParseExact(string, string, SystemIFormatProvider, System.Globalization.DateTimeStyles, out System.DateTime) has some invalid arguments.