8
votes

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.

8
Where is the data coming from - if user entry surely better to use a Date field to fully validate it -as a regex won't capture correct 29th Feb etcmmmmmm
Do you just want syntactically valid dates or do you want actual valid dates? For example, should the regex reject 30-Feb-2012 because it's an impossible date?BunjiquoBianco
I just need syntactically valid dates, although it would be better if regex completely validates it.IrfanRaza
I am using textbox with jquery datepicker to get date.IrfanRaza

8 Answers

26
votes

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

9
votes

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}$
2
votes

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.

1
votes
"\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

3
votes

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);
0
votes

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 :)

0
votes
"^(([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}))$"
-1
votes
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
    }
}