Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what will be the regular expression of date format 'dd-M-y'

what will be the regular expression of date format like '01-Aug-12'.

I have the date format of dd-M-y in asp.net application and want to validate it from asp.net regular expression validator control.

like image 802
Talha Avatar asked Nov 30 '25 05:11

Talha


1 Answers

A very basic format check would be:

\d{2}-[A-Za-z]{3}-\d{2} 

See for yourself here.

To actually validate, we need a day check like @Brijesh Gandhi suggested and then add a complete month list like this:

([12]\d|0[1-9]|3[0-1])-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\d{2}

If you want to allow for lowercase months like aug, you can add the case-insensivity modifier ?i: like this...

([12]\d|0[1-9]|3[0-1])-(?i:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\d{2}

...but that will also allow a month like e.g. aUg - it might be most correct to only allow the first character to be upper- or lowercase.

([12]\d|0[1-9]|3[0-1])-([Jj]an|[Ff]eb|[Mn]ar|[Aa]pr|[Mm]ay|[Jj]un|[Jj]ul|[Aa]ug|[Ss]ep|[Oo]ct|[Nn]ov|[Dd]ec)-\d{2}

See this final version in action here.

Please note that this still will not detect invalid dates like 30-Feb-12. A regexp not accepting those special dates will be very long/ugly. Even using Javascript's Date.parse(...)/new Date(...) will not help detect those as it happily accepts the above mentioned, non-existant date, and return the 1st of March. So to be 100% correct, you need to either do complex coding yourself, or use a library like datejs.

Edit 1: Shortened @Brijesh Gandhi's day check a bit, updated Regexr link.

Edit 2: Remark on correctness.

like image 157
zb226 Avatar answered Dec 02 '25 17:12

zb226



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!