|
-
Jun 13th, 2007, 03:14 PM
#1
Thread Starter
Hyperactive Member
[2.0] Validating a string for MM/DD/YYYY format
Howdy folks,
I have a string variable that should have data in it as 06/12/2007 (MM/DD/YYYY). basically a date. how can I validate if it is in the correct format. If the string variable has data in any other format say 06/12/07 (MM/DD/YY), it should throw an error.
How can I do that in C#?
thanks
nath
-
Jun 14th, 2007, 12:32 AM
#2
Re: [2.0] Validating a string for MM/DD/YYYY format
You have two choices. DateTime.TryParseExact will parse the string and return False if it fails, while ParseExact will throw an exception.
-
Jun 15th, 2007, 04:47 AM
#3
Re: [2.0] Validating a string for MM/DD/YYYY format
You can also use regular expressions:
Code:
private bool IsFormattedDate(string Expression)
{
return System.Text.RegularExpressions.Regex.IsMatch("" , @"(?<Month>([1-9])|(0[1-9])|(1[0-2]))/(?<Day>\d{2})/(?<Year>(?:\d{4}))(?x)");
}
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|