2
votes

I have a regular expression validator on a text box to validate that the text entered is a valid date.

See reg ex below:

ValidationExpression="^[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}$"

Now I want to allow the following in the textbox: mm/dd/yyyy How can I update my regex so that if mm/dd/yyyy is entered it does not throw a validation error?

Thanks in advance.

2
If you really want to catch a valid date you should ensure that month is not greater than 12 and day is not greater than 31 or your day is not zero ... - cristian
I totally agree. I'm currently doing this validation server side :( - Riain McAtamney

2 Answers

7
votes
ValidationExpression="^[0-9m]{1,2}/[0-9d]{1,2}/[0-9y]{4}$"

Basically allows 0-9 or m in the first field, 0-9 or d in the second, 0-9 or y in the third (in regular expression [] brackets contain a list of possible options, - denote ranges of values when placed within brackets).

0
votes

This is a more accurate way to restrict the Date to a more meaningful format

^[1-12]{1,2}/[1-31]{1,2}/[2000-2050,1900-1999]{4}$

This is still not perfect as this will allow - for instance - a date 02/31/2013. Just realized this is quiet buggy.