Need regular expression for validating date in dd-MMM-yyyy format

I am not expert in writing regular expressions so need your help. I want to validate date in “dd-MMM-yyyy” format i.e. 07-Jun-2012. I am using RegularExpressionValidator in asp.net.

Can anybody help me out providing the expression?

Thanks for sharing your time.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Using a DatePicker is probably the best approach. However, since that’s not what you asked, here’s an option (although it’s case sensitive):

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

In addition, here’s a place you can easily test Regular Expressions: http://www.regular-expressions.info/javascriptexample.html

Method 2

Regex without leading zero in day.

^d{1,2}-[a-zA-Z]{3}-d{4}$

Update Regex with leading zero in day.

^d{2}-[a-zA-Z]{3}-d{4}$

Method 3

It’s not regex, but you can use build in DateTime.TryParseExact function to validate your datetime string

DateTime dateTime;
string toValidate = "01-Feb-2000";

bool isStringValid = DateTime.TryParseExact(
    toValidate,
    "dd-MMM-yyyy",
    CultureInfo.InvariantCulture,
    DateTimeStyles.None,
    out dateTime);

Method 4

The accepted solution allows ’00’ as the day, so here is a fix for that:

^(([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}$

Notes/Exceptions:

1.Be aware of case sensitivity issues. Eg. ‘DEC’ will not pass while ‘Dec’ will pass. You may want to convert the regex string and test string to lowercase before testing (if your application allows).

2.This will not catch days that don’t exist, like Feb 30th, June 31st, etc.

Method 5

"d{4}d{2}d{2}|d{2}/d{2}/d{4}|d{2}.d{2}.d{4}|d{2}-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)|(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-d{2}-d{4}|d{4}-d{2}-d{2}"

These format is mm.dd.yyyy, d-MMM, mm.dd.yyyy

Method 6

Yet another idea would be to try this (similar to user1441894’s idea):

var x = DateTime.Parse("30-Feb").GetDateTimeFormats();

I learned to use this yesterday (for a different purpose). So try-catch this statement to deal with validity/invalidity of the date 🙂

Method 7

"^(([1-9]|0[1-9]|1[0-9]|2[1-9]|3[0-1])[-]([JAN|FEB|MAR|APR|MAY|JUN|JULY|AUG|SEP|OCT|NOV|DEC])[-](d{4}))$"

Method 8

using System.Text.RegularExpressions

private void fnValidateDateFormat(string strStartDate,string strEndDate)
{
    Regex regexDt = new Regex("(^(((([1-9])|([0][1-9])|([1-2][0-9])|(30))\-([A,a][P,p][R,r]|[J,j][U,u][N,n]|[S,s][E,e][P,p]|[N,n][O,o][V,v]))|((([1-9])|([0][1-9])|([1-2][0-9])|([3][0-1]))\-([J,j][A,a][N,n]|[M,m][A,a][R,r]|[M,m][A,a][Y,y]|[J,j][U,u][L,l]|[A,a][U,u][G,g]|[O,o][C,c][T,t]|[D,d][E,e][C,c])))\-[0-9]{4}$)|(^(([1-9])|([0][1-9])|([1][0-9])|([2][0-8]))\-([F,f][E,e][B,b])\-[0-9]{2}(([02468][1235679])|([13579][01345789]))$)|(^(([1-9])|([0][1-9])|([1][0-9])|([2][0-9]))\-([F,f][E,e][B,b])\-[0-9]{2}(([02468][048])|([13579][26]))$)");

    Match mtStartDt = Regex.Match(strStartDate,regexDt.ToString());
    Match mtEndDt   = Regex.Match(strEndDate,regexDt.ToString());
    if (mtStartDt.Success && mtEndDt.Success)
    {
            //piece of code
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x