Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i need a regex expression which should be able to validate february in leap years i.e i should not be able to submit the page if the user gives 28-feb-2016 for a leap year.


Thanks

What I have tried:

This is what i have tried


^(([1-9])|([0][1-9])|([1-2][0-9])|([3][0-1]))\-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\-\d{4}$
Posted
Updated 16-May-17 23:28pm

1 solution

Quote:
i need a regex expression which should be able to validate february in leap years i.e i should not be able to submit the page if the user gives 28-feb-2016 for a leap year.

In this case, you also need to check for months with 31 days and reject '31 Jun'. Last time I checked, '28 Feb' was correct every year, leap or not.
Even if possible, what you want to do with RegEx is kind of abuse.

I would use RegEx to check date format roughly '99 aaa 9999', and then a piece of JS code once I know the format is correct.

The problem with checking deep details with RegEx, is that the RegEx will degenerate as you introduce those details. The problem is that you can't include calculus in the RegEx, and thus you have to describe every possibility that don't fit in general case.
Once you know the input match the general format of a date, a little 'simple) piece of JS code can check if year is leap or not (year % 4 == 0)

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
 
Share this answer
 
Comments
CPallini 17-May-17 5:58am    
5.
Patrice T 17-May-17 5:59am    
Thank you
Matt T Heffron 17-May-17 12:24pm    
The right tool for the job.
5
Patrice T 17-May-17 12:34pm    
Thank you

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