Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I require a regular expression for date entry such that it allows following three conditions

1.)11/11/14
2.)11/11
3.)11

User can enter anything,I mean entire date(1st case),date with month(2nd case) and only date(3rd case).

I have a RegEx
\d{1,2}\/\d{1,2}\/\d{1,4}
This allows only 1st case.

Can anyone help me getting all 3 cases right.

P.S. Am using C#.net

Thanks in advance
Posted

1 solution

Try:
(?<day>\d{1,2})(\/(?<month>\d{1,2})(\/(?<year>\d{1,4}))?)?


[edit]"HTML" swallowed[/edit]
 
Share this answer
 
v2
Comments
Brian A Stephens 18-Dec-14 15:27pm    
It still allows for one-digit and three-digit years, as the OP requested. Not sure what dates he's expecting, but (\d{2}){1,2} might be better.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900