locked
Regular Expression with Date Format

    Question

  • Would anyone know of a Regular expression that will check for a valid date, but not include the time? 

     

    I'm looking for mm/dd/yyyy.  If the user were to include time, I want to display an error.  After trying some Regular Expressions that were online, they still accept the time which is something I don't need.

    Monday, May 05, 2008 5:07 PM

Answers

All replies

  • I have a follow-up question.  It seems that the Regular Expression I have run into searches for a date in the mm/dd/yyyy format, but once it does, it simply doesn't care about anything else after that.  Is there a way in a regular expression to say "If you find any character after the valid expression, still consider the whole expression false?"

     

    Here is the Regular expression I am currently trying to use:

     

    (0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d

     

     

    Here is the website where I found the Reg Ex:

     

    http://www.regular-expressions.info/regexbuddy/datemmddyyyy.html

    Monday, May 05, 2008 5:36 PM

  • >"If you find any character after the valid expression, still consider the whole expression false?"

    You can use the start "^" and end "$" of string literals.

    ^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$

    One thing I must say about this regex is it's not guaranteeed to be a valid date. There are regexs that will, but they are kind of big since it has to accout for the number of days in each month and leap year.
    Monday, May 05, 2008 6:13 PM
  • Thanks for the String Anchor info!

     

    Would you happen to know of any Reg Expressions that would account for leap day?

    Monday, May 05, 2008 7:03 PM
  • Regexlib has nice descriptions. Here's one by Michael Ash that handles a lot of formats...

    http://regexlib.com/REDetails.aspx?regexp_id=610

    or search by mm/dd/yyyy

    http://regexlib.com/Search.aspx?k=mm%2fdd%2fyyyy&c=5&m=-1&ps=20

    Monday, May 05, 2008 7:29 PM
  •  

    is there a way to make this expression ^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$ also work with MM/YYYY format as well? I have a form where our users need to enter either MM/DD/YYYY or MM/YYYY. I have tried changing the expression to  ^(0[1-9]|1[012])([/]|.*)((0[1-9]|[12][0-9]|3[01])|.*)[/](19|20)\d\d$ by adding ".*"  which works, but then the user could really enter anything into the day part.

    Thursday, May 08, 2008 3:04 PM
  • wagswuv. try this...
     
    ^(0[1-9]|1[012])[- /.]((0[1-9]|[12][0-9]|3[01])[- /.])?(19|20)\d\d$


    I put paranthesis around the dd[-/.] part, followed by the ?. Means that 0 or 1 occurence of the dd[-/.] is allowed.
    Thursday, May 08, 2008 3:44 PM
  • If you are going to allow -/. as seperators then you may want to enfore that the same one be used throughout.  This can be accomplished with backreferences as can be seen in this modified version of Joe's regex:

    Code Snippet

    ^(0[1-9]|1[012])(?<Sep>[- /.])(?:(0[1-9]|[12][0-9]|3[01])\k<Sep>)?(19|20)\d\d$


    Thursday, May 08, 2008 5:40 PM
  •  That worked great. I have another question. How could I make that same expression work with a multi-line textbox? So the user could enter a date each a new line.
    Monday, June 02, 2008 9:48 PM
  • So far I have tried

    ^([\s\S])*((0[1-9]|1[012])[/]((0[1-9]|[12][0-9]|3[01])[/])?)?(19|20)\d\d$

    and it kind of works however it allows all white-space character, so you can have spaces and stuff like that. For some reason [/r/n] which are equivalent to a "carriage return" and a "new line" don't seem to work with mulit-line text box. Does somebody have any input on maybe a better way? thanks.

    • Edited by wagswvu Tuesday, June 03, 2008 3:26 PM spelling
    Tuesday, June 03, 2008 3:26 PM
  • Wednesday, June 04, 2008 9:27 PM
  • Thanks for the String Anchor info!

     

    Would you happen to know of any Reg Expressions that would account for leap day?

    ^(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$

     

    that should work for leap year and everything.


    Thanks, Suresh Veeragoni
    Monday, August 15, 2011 2:49 PM
  • Thanks Suresh! 

    I've tried dozens of other expressions for dates.  Your's is the only one I found that works for every invalid date I could think of.

    Friday, November 18, 2011 8:48 PM
  • try this code .... 

      <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>           <asp:RegularExpressionValidator ID="RegularExpressionValidator1"              runat="server" ErrorMessage="DD/MM/YYYY"              ControlToValidate="TextBox1"              ValidationExpression="^(0?[1-9]|[12][0-9]|3[01])[ \/.-](0?[1-9]|1[012])[ \/.-](19|20)\d\d$">          </asp:RegularExpressionValidator>

    Student
    Saturday, January 07, 2012 3:24 PM
  • This is not working when i entered the date below 1600 (for example if you enter date 12/12/1599 then above expression will as invalid date). It is working fine for date greater than 01/01/1600. How to valid date below 1600.
    Wednesday, May 02, 2012 6:39 AM