GibboK
GibboK

Reputation: 73918

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

Upvotes: 10

Views: 35902

Answers (7)

Ajit
Ajit

Reputation: 1

((([0-9][0-9][0-9][1-9])|([1-9][0-9][0-9][0-9])|([0-9][1-9][0-9][0-9])|([0-9][0-9][1-9][0-9]))-((0[13578])|(1[02]))-((0[1-9])|([12][0-9])|(3[01])))|((([0-9][0-9][0-9][1-9])|([1-9][0-9][0-9][0-9])|([0-9][1-9][0-9][0-9])|([0-9][0-9][1-9][0-9]))-((0[469])|11)-((0[1-9])|([12][0-9])|(30)))|(((000[48])|([0-9]0-9)|([0-9][1-9][02468][048])|([1-9][0-9][02468][048]))-02-((0[1-9])|([12][0-9])))|((([0-9][0-9][0-9][1-9])|([1-9][0-9][0-9][0-9])|([0-9][1-9][0-9][0-9])|([0-9][0-9][1-9][0-9]))-02-((0[1-9])|([1][0-9])|([2][0-8])))

This is the regex for yyyy-MM-dd format.

You can replace - with \/ for yyyy/MM/dd...

Tested working perfect..

Upvotes: -1

Mr Z
Mr Z

Reputation: 101

I would like to add a little change in Spudley's answer:

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

so you can use date like 2013-5-5 (month and date is not necessary the zero but can be used)

Hope it helps.

Upvotes: 3

Menaka Sankar
Menaka Sankar

Reputation: 189

(19|20)[0-9]{2}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])

mach result:

1999-09-12

Upvotes: 0

gpresland
gpresland

Reputation: 1819

Another implementation for ISO 8601 structured dates:

^\d{4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2}.?\d{0,}$

It's not quite as strict, and will accept incorrect dates, but it should validate that it follows the ISO 8601 structure even if the date is a non-existent one. It should also be fairly simple to understand for anyone with a basic Regex understanding.

If you really want to ensure the date is correct, and work with it, run DateTime.TryParse() on it.

Upvotes: 0

Ralph177
Ralph177

Reputation: 201

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.

Upvotes: 10

Spudley
Spudley

Reputation: 168685

Here's one possible regex:

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

Note: this will prevent months >12 and days >31, but won't check specific months for length (ie it won't block 30th Feb or 31st Apr). You could write a regex to do that, but it would be quite lengthy, and 29th Feb is always going to give you problems in regex.

I'd say if you need that kind of fine-grained validation, you're better off parsing the date with a date library; regex isn't the tool for you. This regex should be sufficient for basic pre-validation though.

I've also gone lenient on the year; just checking that it's four digits. But if you want some sort of sanity check (ie within certain bounds), it shouldn't be too hard to add. Foe example, if you want to match only dates in the this century, you would replace the \d{4} at the beginning of the regex with 20\d{2}. Again, trying to validate a date with excessive accuracy in regex is going to be difficult and you should use a date parser, but you can get basic century-level matching quite easily to prevent the user entering anything really silly.

Finally, I've put ^ and $ to tie off the ends of the string so it can't match if the user enters a valid date and extra characters as well. (You may want to add a string length validator for this as well).

Hope that helps.

Upvotes: 17

redent84
redent84

Reputation: 19239

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

Upvotes: 5

Related Questions