Regular Expression (Regex) for dd/mm format

I have a text field and want to implement regular expression to validate mm/dd format. I have a regex currently which works for this format but not able to validate against months like 29th February, 30th April etc.. How to modify the regex to include this month validation?

Currently regex I'm using is - (0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])
bujji bongaramAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dr. KlahnPrincipal Software EngineerCommented:
Such a regex, if implemented 100% correctly, would be exceedingly complicated due to handling of the leap year problem.

How correct does the regex need to be?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bujji bongaramAuthor Commented:
Leap year calculation is not required.
Dr. KlahnPrincipal Software EngineerCommented:
[([01|03|05|07|08|10|12]/[01-31])|([04|06|09|11]/[01-30])|(02/[01-29])]

(31-day months)/01-31 alternated with (30-day months)/01-30 alternated with (February)/01-29
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

bujji bongaramAuthor Commented:
Dr Klahn, does this cover for days (dd) also? Looks to me like mm only?
Dr. KlahnPrincipal Software EngineerCommented:
Months and days both.  Consider January 31st.

01/31 matches at

[([01|03|05|07|08|10|12]/[01-31])|([04|06|09|11]/[01-30])|(02/[01-29])]
ozoCommented:
[01-31] matches any character of: '0', '1' to '3', '1'
which is the same as [0-3]  any character of: '0' to '3'
which may not be what you intended
You may have meant (0[1-9]|[12][0-9]|3[01])
bujji bongaramAuthor Commented:
I tested and it is not working "klhan". I tried 02/29 it throws error which is correct but tried 02/28 still throwing error. Tried 04/30 which should not throw error but I get error.
awking00Information Technology SpecialistCommented:
Rather than using regular expressions, you might consider try using a SimpleDateFormat with the Parse method to validate and capture exceptions. Note, however, both that method and regular expressions will validate 07/06 as July 6th even if the source intended it to be June 7th.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.