MM/YYYY format

barski

Junior Contributor
Joined
Apr 7, 2002
Messages
240
Location
Tennessee
First off I know little to nothing about regular expressions. I read a couple of articles today but can't quite get it working. For validation purposes i only want to accept values like 1/2004 or 12/2004. I just can't figure how to adjust for one or two digits.

periodExpression = new Regex(@"^[1-9]/\d{4}$");
periodTwoExpression = new Regex(@"^[1-2][0-2]/\d{4}$");

the above "works" in a way but wouldn't prevent 22/5006 nevermind the fact that i have to create two patterns
 
I don't know what are acceptable date ranges are but the following regex will take a valid month and any year in the 20th or 21st century:


^([1][0-2]|[0][1-9])/(19|20)\d{2}
 
Back
Top