Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression to detect yyyy-MM-dd

I use asp.net 4 and c#.

I need to use a WebControl of type Validation namely RegularExpressionValidator to detect data inputed in a TextBox that IS NOT in format yyyy-MM-dd (String).

Any idea how to write the RegEx to apply ot this control?

Thanks

like image 824
GibboK Avatar asked Mar 09 '11 14:03

GibboK


People also ask

What is regular expression for date format dd mm yyyy?

For dd-mm-yyyy format, use ^(0[1-9]|[12][0-9]|3[01])[- /.] (0[1-9]|1[012])[- /.]

What is the regular expression for date format?

The regex matches on a date with the YYYY/MM/DD format and a "Date of birth:" or "Birthday:" prefix (Year min: 1900, Year max: 2020). For example: Date of birth: 1900/12/01.

How do you check if the date is in YYYY MM DD format in PHP?

isValid() function checks if the given string is a valid date using PHP. It uses DateTime class of PHP to validate the date according to the specified format. This function returns TRUE if the date is valid, otherwise FALSE.


2 Answers

Spudley's answer above allows 00 for day and month.

I fixed it :

^\d{4}-((0[1-9])|(1[012]))-((0[1-9]|[12]\d)|3[01])$

Note: neither of these expressions check for days in a month that are invalid, e.g. 04/31, 06/31 or 02/29 on non-leap years.

like image 94
Ralph177 Avatar answered Sep 22 '22 11:09

Ralph177


Regular expression \d\d\d\d-\d\d-\d\d should do the trick.

like image 32
redent84 Avatar answered Sep 18 '22 11:09

redent84