Regular expression in C # .net

I need a function to check the entered date. whether the entered date is in the correct format. I browsed the web and got a regex. His work is great, except when you enter 12/12 / YYYY (in any year), he shows an error stating that this is not a valid date.

bool IsDate(string date) { Match dobMatch = Regex.Match(date, @"^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$"); if (!dobMatch.Success) {return true;} else {return false;} } 

i

+4
source share
5 answers

Try DateTime.TryParse() .

This allows you to spend time. This method allows you to pass CultureInfo if the culture you want to use to parse the date.

Alternatively, if you really want to use regular expressions, see http://regexlib.com/ . . Comprehensive regex library with regular expression ratings.

+6
source

Why are you using regex for it?

Try the following: Confirm DateTime in C #

+4
source

Why don't you use DateTime.TryParse or DateTime.TryParseExact is much easier than using a regular expression.

+2
source

use (? [0] [1-9] | [1 | 2] [0-9] | [3] [0 | 1]) ^. / -. / - $

or go to the following link, http://regexlib.com/DisplayPatterns.aspx?cattabindex=4&categoryId=5

+1
source

The regular expression only checks the character data in the string and says nothing about the actual validity of the data in this string.

To check the string as a valid date in .Net, use the following:

 DateTime dt; if (DateTime.TryParse(someString, out dt)) { //Parsed as a valid date whose value is now in the variable dt } else { //Not a valid date } 
+1
source

All Articles