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
For dd-mm-yyyy format, use ^(0[1-9]|[12][0-9]|3[01])[- /.] (0[1-9]|1[012])[- /.]
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.
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.
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.
Regular expression \d\d\d\d-\d\d-\d\d
should do the trick.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With