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

后端 未结 8 1317
不思量自难忘°
不思量自难忘° 2020-12-31 17:57

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 i

相关标签:
8条回答
  • 2020-12-31 18:01
    "^(([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}))$"
    
    0 讨论(0)
  • 2020-12-31 18:03

    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 讨论(0)
  • 2020-12-31 18:05

    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.

    0 讨论(0)
  • 2020-12-31 18:13

    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 讨论(0)
  • 2020-12-31 18:13
    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
        }
    }
    
    0 讨论(0)
  • 2020-12-31 18:14

    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

    0 讨论(0)
提交回复
热议问题