MM/YYYY format

barski

Well-known member
Joined
Apr 7, 2002
Messages
239
Location
Tennessee
First off I know little to nothing about regular expressions. I read a couple of articles today but cant quite get it working. For validation purposes i only want to accept values like 1/2004 or 12/2004. I just cant 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 wouldnt prevent 22/5006 nevermind the fact that i have to create two patterns
 
I dont 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