RegExLib.com - The first Regular Expression Library on the Web!

Please support RegExLib Sponsors

Sponsors

Regular Expression Details

Title Test Find DateTime M/d/y hh:mm:ss
Expression
^(?=\d)(?:(?:(?:(?:(?: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}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$
Description
DateTime Validator.
Matches
12/25/2003 | 08:03:31 | 02/29/2004 12 AM
Non-Matches
02/29/2003 1:34 PM | 13:23 PM | 24:00:00
Author Rating: The rating for this expression. Michael Ash
Source Michael Ash
Your Rating
Bad Good

Enter New Comment

Title
 
Name
 
Comment
 
Spammers suck - we apologize. Please enter the text shown below to enable your comment (not case sensitive - try as many times as you need to if the first ones are too hard):

Existing User Comments

Title: Simple DateTime
Name: Tina
Date: 6/8/2018 5:58:09 PM
Comment:
I need something much simpler for use in Access. I don't need it to validate as the user will be keying in the DateTime from an email. I will be using this to Index emails into our document management system and can only have 255 total characters. The user will also be keying in the document name with the date and time. For example the document might be named something like this: 165502_227_2018_EMAILCORR mm-dd-YYYY 12:18 PM


Title: Simple DateTime
Name: Tina
Date: 6/8/2018 5:57:57 PM
Comment:
I need something much simpler for use in Access. I don't need it to validate as the user will be keying in the DateTime from an email. I will be using this to Index emails into our document management system and can only have 255 total characters. The user will also be keying in the document name with the date and time. For example the document might be named something like this: 165502_227_2018_EMAILCORR mm-dd-YYYY 12:18 PM


Title: Simple DateTime
Name: Tina
Date: 6/8/2018 5:55:22 PM
Comment:
I need something much simpler for use in Access. I don't need it to validate as the user will be keying in the DateTime from an email. I will be using this to Index emails into our document management system and can only have 255 total characters. The user will also be keying in the document name with the date and time. For example the document might be named something like this: 165502_227_2018_EMAILCORR mm-dd-YYYY 12:18 PM


Title: This is great, does exactly what I need
Name: Bob Bailey
Date: 4/6/2014 6:58:28 PM
Comment:
Very complicated expression that I guess I could have built myself along with many hours and much cursing. Thank you, Michael Ash, for saving me and my family from that. :) ...Bob


Title: commonprosperity.org
Name: www.shun-happymall.com
Date: 10/28/2012 11:19:48 PM
Comment:
http://www.commonprosperity.org/ Dear friends, do you want to have some different things? Whether you want to give your relatives and friends, take a few different exotic gifts? Whether you want to buy some cheap benefits of thing? So please, let us begin now! Click on our website (http://www.commonprosperity.org/) Will bring you different surprise,[email protected] ............ ./′ˉ/'...'/ˉ/` ........../'/.../..../...../ˉ\ ........('(...′...′....ˉ~ /') .........\.................'../ ..........\................../ ............\..............( ..............\.............\....


Title: Date validation
Name: om
Date: 7/17/2012 5:30:24 AM
Comment:
6/19/1740 test


Title: High definition<a href="http://www.beatsbydrdrer.com/"><strong>Cheap Beats By Dre</strong></a> are precision- engineered to reveal the full sound of
Name: Beats By Dre Sale
Date: 6/12/2012 3:09:02 AM
Comment:
High definition<a href="http://www.beatsbydrdrer.com/"><strong>Cheap Beats By Dre</strong></a> are precision- engineered to reveal the full sound of today's digital music.<a href="http://www.beatsbydrdrer.com/"><strong>Beats By Dre Sale</strong></a> of high quality deserve you to buy, high definition<a href="http://www.beatsbydrdrer.com/"><strong>Beats By Dre Studio</strong></a> with free shipping for you to shop in our Online store.


Title: Validation Failed
Name: José Dunstan
Date: 6/10/2010 10:21:41 AM
Comment:
I tried your regular expressions. But when querying by date 02/13/2010 or 26/02/2010 as the error states as


Title: Newbie RegEx User Says "Thank You!"
Name: Cortster
Date: 1/13/2010 2:14:52 PM
Comment:
Thanks, Michael, for taking the time to create this regEx.


Title: Problem to restrict special characters
Name: Sam
Date: 8/26/2009 5:34:22 AM
Comment:
I have used same regular expression for entering data in textbox. When i was run my application in IE it does not permit to add following special character 1. & 2. ( 3. % But when we run on Mozilla then I can enter these special characters in text box. How to restrict these characters in Mozilla. Please help me Its urgent.....


Title: Works Great
Name: Ali
Date: 3/16/2007 9:23:33 AM
Comment:
Nice work


Title: Many tanks
Name: Steve
Date: 7/8/2006 11:49:29 AM
Comment:
Michael, sincere thanks for your work.


Title: Many tanks
Name: Steve
Date: 7/8/2006 11:49:15 AM
Comment:
Michael, sincere thanks for your work.


Title: Re: field validation
Name: Michael Ash
Date: 5/10/2005 8:36:16 AM
Comment:
[-\d]* will match what you said you wanted. I don't know if that's what you meant. It will match nothing, hyphens, numbers or numbers with hyphens.


Title: field validation
Name: vinay
Date: 5/7/2005 2:05:56 AM
Comment:
I need the regex for a field validation i.e.. the text field should accept only no's and hyphen's in any format .


Title: Nice
Name: Anders Bohlin
Date: 3/16/2005 10:39:15 AM
Comment:
Works like clockwork.... and i know about these things since im from switzerland.... :D thanx


Title: Re:
Name: Praphulla
Date: 12/27/2004 4:02:18 AM
Comment:
Hi Michael, Yeah! I guessed the answer.But posted to clarify. Thank You for the regular expression code.Reduced many lines of scripting and Thank you for clarifying my doubt.


Title: PostDate
Name: Praphulla
Date: 12/26/2004 1:42:28 AM
Comment:
Hai Micheal, I want to use the validator for the DateOfBirth field.so i don't want to accept the date that is greater than the system date. How can i now proceed to check that


Title: Re: What about the post date
Name: Michael Ash
Date: 12/25/2004 12:13:39 AM
Comment:
I am sorry but I do not understand your comment. I don't know what you mean by post date. And what about the year 2010? Did you expect it not to accept? Why?


Title: What about the post date
Name: Praphulla
Date: 12/24/2004 8:08:19 AM
Comment:
I think its not handling the post date.If i choose year as 2010.Its accepting.


Title: Good
Name: XXX
Date: 12/14/2004 7:47:08 AM
Comment:
Very nice to see the regular expression.Thank You.Lot of code will be reduced


Title: Very Good
Name: Rui Veloso
Date: 10/5/2004 10:56:53 AM
Comment:
I wated a regukar expression just for validating time and this one is just very good for what i wated it for thanks Michael


Title: Thank you!
Name: Vlad
Date: 7/19/2004 1:02:39 AM
Comment:
Michael, Thank you!


Title: Re: need help splitting into 2 RE
Name: Michael Ash
Date: 7/19/2004 12:02:07 AM
Comment:
There are forced CRLF on this page. The regex should be one long string, no line breaks. http://regexlib.com/REDetails.aspx?regexp_id=113 is a mm/dd/yyyy date regex. It accept two digit years but removing the question marks before both \d{2} you can make it accept only 4 year dates


Title: need help splitting into 2 RE
Name: Vlad
Date: 7/18/2004 6:05:21 PM
Comment:
Michael, I tried using the date expression(with out time check) and it's not working. I think there's a missing parenthesis somewhere and I can't find it. Also, I made a typo in my previous post, I meant to say mm/dd/yyyy; could you flip it around? Thanks, Vlad


Title: Re: need help splitting into 2 RE
Name: Michael Ash
Date: 7/16/2004 9:31:29 PM
Comment:
Here is the time portion of this regex http://regexlib.com/REDetails.aspx?regexp_id=370 I've posted a dd/mm/yyyy regex here http://regexlib.com/REDetails.aspx?regexp_id=505 However this regex is geared toward .Net so it may not work with another language. Here is the regex with the time check removed (?n:^(?=\d)((?<day>31(?!(.0?[2469]|11))|30(?!.0?2)|29(?(.0?2)(?=.{3,4}(1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(16|[2468][048]|[3579][26])00))|0?[1-9]|1\d|2[0-8])(?<sep>[/.-])(?<month>0?[1-9]|1[012])\2(?<year>(1[6-9]|[2-9]\d)\d{2}))$) If you need a more language neutral version look here http://blogs.regexadvice.com/mash/archive/2004/06/17/1269.aspx


Title: need help splitting into 2 RE
Name: Vlad
Date: 7/16/2004 7:23:47 PM
Comment:
Hi, I need to have two separate validations. one for dd/mm/yyyy and the other fot hh:mm:ss I was trying to split it up this RE but with no luck. Can you help me out? Thanks, Vlad


Title: re: Can i have code to validate dd/mm/yyyy
Name: Michael Ash
Date: 5/10/2004 9:51:54 AM
Comment:
Please refer to http://blogs.regexadvice.com/mash/posts/1021.aspx To require leading zeroes remove the question mark right after the zero. 0? -> 0


Title: Can i have code to validate dd/mm/yyyy
Name: Mufaddal
Date: 5/8/2004 4:39:03 AM
Comment:
DD/MM/YYYY the validator should not allow without prevailing zeros i mean , it should not validate 1/1/2000 . the correct format should be 01/01/2000


Title: Can i have code to validate dd/mm/yyyy
Name: Mufaddal
Date: 5/8/2004 4:13:58 AM
Comment:
Thanks in advance


Title: the Calendar
Name: Michael Ash
Date: 4/23/2004 2:08:32 AM
Comment:
refer to http://blogs.regexadvice.com/mash/archive/2004/04/23/1021.aspx


Title: re: developer
Name: Michael Ash
Date: 4/22/2004 6:05:01 PM
Comment:
Check your old calendars again. 2/29/2000 is a valid date and was a leap year. It was in all the papers. http://scienceworld.wolfram.com/astronomy/LeapYear.html


Title: developer
Name: joe
Date: 4/22/2004 5:52:47 PM
Comment:
Close! I rated it a 1/5 since date validation routines either work or they don't. There's no middle ground. The year 2000 is NOT a leap year and 2/29/2000 is NOT a valid date.


Title: very good
Name: dulker
Date: 3/25/2004 12:38:05 AM
Comment:
very gooooooood


Title: Thanks
Name: Jensen Bredal
Date: 3/8/2004 7:26:04 AM
Comment:
Many thanks.


Title: re: Great
Name: Michael Ash
Date: 3/4/2004 12:55:24 PM
Comment:
Sorry again. I'll get this right eventualy ^(?=\d)(?:(?:31(?!.(?:0?[2469]|11))|(?:30|29)(?!.0?2)|29(?=.0?2.(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(?:\x20|$))|(?:2[0-8]|1\d|0?[1-9]))([-./])(?:1[012]|0?[1-9])\1(?:1[6-9]|[2-9]\d)?\d\d)?(\x20?((0?[1-9]|1[012])(:[0-5]\d){0,2}(\x20[AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$ Needless to say you should test this thoroughly to see you don't get any false positives.


Title: re: Great
Name: Michael Ash
Date: 3/4/2004 12:29:56 PM
Comment:
Sorry, that last regex had a slight bug in it. Use this one for dd/mm/yy instead ^(?=\d)(?:(?:31(?!.(?:0?[469]|11))|(?:30|29)(?!.0?2)|29(?=.0?2.(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(?:\x20|$))|(?:2[0-8]|1\d|0?[1-9]))([-./])(?:1[012]|0?[1-9])\1(?:1[6-9]|[2-9]\d)?\d\d)?(\x20?((0?[1-9]|1[012])(:[0-5]\d){0,2}(\x20[AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$


Title: re: Great!
Name: Michael Ash
Date: 3/4/2004 12:05:21 PM
Comment:
Sorry Jensen but I'm not really an ASP.Net programmer. I only learned how to implement the asp:RegularExpressionValidator yesterday. I don't know what all the attributes do, so i don't really know what 'static' does in regard to the validator. I just modified some sample code I found. But assuming you can modify the regex the control uses on the fly then I think you had the right approach to to handling dd/mm/yy. Just use this regex for d/m/y ^(?=\d)(?:(?:31(?!.(?:0?[469]|11))|(?:30|29)(?!.0?2)|29(?=.0?2.(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:2[0-8]|1\d|0?[1-9]))([-./])(?:1[012]|0?[1-9])\1(?:1[6-9]|[2-9]\d)?\d\d)?(\x20?((0?[1-9]|1[012])(:[0-5]\d){0,2}(\x20[AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$


Title: Great!
Name: Jensen bredal
Date: 3/4/2004 4:28:45 AM
Comment:
Well that is what i did . Does display need to be "static" . I use dynamic. I also wnat my validator to work as well for german, danish or norwegian datetime. The problem is that the use dd-mm-yyyy or dd.mm.yyyy. My first approch was to detect the user language preference and then set the ValidateExpression property of my regular expresssion validator property to the appropriate expression. And i want all tis to happen on the client side. So far , the "en" (english) one is the only one that works with javascript. I'am rigth and how could i solve this. Many thanks Jensen Bredal


Title: Re: Implementation Code
Name: Michael Ash
Date: 3/3/2004 3:12:33 PM
Comment:
<asp:Label runat="server">Enter date or time</asp:Label><asp:TextBox id="Textbox1" runat="server"></asp:TextBox> <asp:RegularExpressionValidator id="RegularExpressionValidator1" runat="server" ControlToValidate="Textbox1" ValidationExpression="^(?=\d)(?:(?:(?:(?:(?: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}))($| (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}( [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$" Display="Static" Font-Name="verdana" Font-Size="10pt"> Please enter a valid date </asp:RegularExpressionValidator>


Title: Implementation Code
Name: Jensen bredal
Date: 3/3/2004 5:14:55 AM
Comment:
could you provide a sample code for using this to validate a date in an asp.net Text box control? Thanks Jensen


Title: Format
Name: Michael Ash
Date: 9/1/2003 2:24:56 AM
Comment:
I mistakenly said originally that this used a dd/mm/yyyy format. It uses a mm/dd/yyyy. I apologize for the confusion. There are variations of my original Date validating RE on this library that check both d/m/y and y/m/d. I posted the time portion separately so it could be appended to those RE's


Title: dd/mm/yyyy
Name: Mars Yau
Date: 8/30/2003 12:16:42 AM
Comment:
It is not in format dd/mm/yyyy? how can i modify it to support this format?


Title: AM PM
Name: Sheik
Date: 8/27/2003 8:49:42 PM
Comment:
This modification allows AM and PM to be entered in either Upper Case or Lower Case ^(?:\s+)?(?=\d)(?:(?:(?:(?:(?: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}))($| (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(( [AP]M)|( [ap]m)))|([01]\d|2[0-3])(:[0-5]\d){1,2})?(?:\s+)?$


Title: Works Great
Name: Sheik
Date: 8/27/2003 8:37:56 PM
Comment:
Please try the following Regular Expression. ^(?:\s+)?(?=\d)(?:(?:(?:(?:(?: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}))($| (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}( [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?(?:\s+)?$ I have just added the string (?:\s+)? before and after the original expression to trim spaces before and after. It works great. Thanks


Title: Re: doesnt validate dates period!!
Name: Michael Ash
Date: 8/27/2003 1:39:57 PM
Comment:
Worked fine with I tested those dates. 8/27/2003 and 9/27/2003. Make sure you don't have any leading or trailing spaces (or LF/CR)


Title: correction:
Name: shane
Date: 8/27/2003 11:47:25 AM
Comment:
excuse me; 8/27/2003


Title: doesnt validate dates period!!
Name: shane
Date: 8/27/2003 11:46:33 AM
Comment:
this expression doesnt even work. wouldnt validate todays date: 9/27/2003


Title: Re: Does not validate leap years correctly
Name: Right
Date: 8/26/2003 12:20:12 PM
Comment:
nice


Title: Re: Does not validate leap years correctly
Name: Michael Ash
Date: 8/26/2003 10:58:16 AM
Comment:
This Re validate DATES, 01/01/2100 is a perfectly valid date. By validating leap years I specificly mean the it valid days in Feb. correctly. Feb 29th is only valid in leap years. If you test this RE with 02/29/2100 it will fail, as it should.


Title: Does not validate leap years correctly
Name: Guest
Date: 8/26/2003 10:41:08 AM
Comment:
This regex validates 01/01/2100...which it should not. Leap years only occur when the year is divisible by four, or when it is divisible by both 100 and 400.


Copyright © 2001-2024, RegexAdvice.com | ASP.NET Tutorials