65.9K
CodeProject is changing. Read more.
Home

Validating dates in dd-MMM-yyyy format

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.35/5 (9 votes)

May 13, 2010

CPOL
viewsIcon

28390

using System.Text.RegularExpressionsprivate void fnValidateDateFormat(string strStartDate,string strEndDate){ Regex regexDt = new...

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
	}
}
Above regular expression is specific to dates in dd-MMM-yyyy format. In case you are using more than 1 date format across your application, it would be a good idea to declare either a switch case that returns specific regex objects as per date formats or enums.