We need help for regular expression that work with asp.net asp:RegularExpressionValidator to validate date in MMddyy format. Problem we are facing is leap year. Issue is that is it possible to verify through regular expression that it only accepts valid leap year dates like 02/29/2008 is a valid date but 02/29/2010 is not a valid date.
Any regular expression that works with "asp:RegularExpressionValidator"?
As bitwise says clientside JS is the way to do this not a horrible regex a la Tom's mindboggling post. I've got quite a tidy date validator on my work machine, I'll post on Monday.
If you ever get some kind of failed validation issue with your app can you imagine the nightmare of trying to de-cipher that regex?
OK, you asked for a regex. Here it is. I think it's immediately obvious why it's not a good idea to validate a date with a regular expression:
First, the verbose, commented version to at least make understanding this beast possible:
Or, if you can't use verbose regexes in ASP.NET validators:
These allow but do not require a leading zero in single-digit months/days. If you don't want that, replace all instances of
0?
with0
.If server side is not an option, you'll have to use JavaScript. Try the code posted and explained here. It determines that 02/29/2010 is invalid and 02/29/2008 is valid. Use a CustomValidator to call the JavaScript function when necessary.
Since you need logic to validate leap years, consider using a
CustomValidator
. I put this together relatively quickly, but hopefully you'll get the idea.