We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Regular Expression needed

Medium Priority
362 Views
Last Modified: 2013-11-07
I need a regular expression for an aspx page that will allow ONLY this format for a datetime field:

01/01/2009 05:00 am/AM
01/01/2009 06:00 pm/PM

The one attached works in Expresso, but when I use it with a regular expression validator in vs2005, it doesn't work.
^(((((0[13578])|([13578])|(1[02]))[\-\/\s]?((0[1-9])|([1-9])|([1-2][0-9])|(3[01])))|((([469])|(11))[\-\/\s]?((0[1-9])|([1-9])|([1-2][0-9])|(30)))|((02|2)[\-\/\s]?((0[1-9])|([1-9])|([1-2][0-9]))))[\-\/\s]?\d{4})(\s(((0[1-9])|([1-9])|(1[0-2]))\:([0-5][0-9])((\s)|(\:([0-5][0-9])\s))([AM|PM|am|pm]{2,2})))?$

Open in new window

Comment
Watch Question

Since your using C# you need to replace \ (single backslash) with \\ (double backslash)

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Carla RomereDirector of Information Technology

Author

Commented:
I replaced all the single backslashes with double backslashes and it still isn't working correctly. Calls it invalid when it's not.
Ionut A. TudorPHP Programmer
CERTIFIED EXPERT

Commented:
i think you should replace \/ with / like below
^(((((0[13578])|([13578])|(1[02]))[\-/\s]?((0[1-9])|([1-9])|([1-2][0-9])|(3[01])))|((([469])|(11))[\-/\s]?((0[1-9])|([1-9])|([1-2][0-9])|(30)))|((02|2)[\-/\s]?((0[1-9])|([1-9])|([1-2][0-9]))))[\-/\s]?\d{4})(\s(((0[1-9])|([1-9])|(1[0-2]))\:([0-5][0-9])((\s)|(\:([0-5][0-9])\s))([AM|PM|am|pm]{2,2})))?$

Open in new window

Carla RomereDirector of Information Technology

Author

Commented:
When I replaced with that string (al3cs12), it lets anything through and is not preventing the user from submitting the form (which will ultimately time out if the input is incorrect).

The user must enter their datetime as 02/01/2009 6:00 am/pm (AM/PM) or the page times out.
(0[13578])|([13578])  could be written as
(0?[13578])

(0[1-9])|([1-9])  could be written as
(0?[1-9])

(02|2) could be written as
(0?2)

replace all \/ by /
and all \: by :

following requires 2 spaces
(\s)|(\:([0-5][0-9])\s))   I assume you mean
(:[0-5][0-9])?\s

are you aware that
([AM|PM|am|pm]{2,2})
never matches am or AM or PM, nor matches am/AM, I guess you mean
([aApP][mM]/[aApP][mM])

You say that "The user must enter", but we read at the end of your regex
)?$
which means that the whole match is optional.

Also, why do you use so much grouped matches?
Carla RomereDirector of Information Technology

Author

Commented:
Here is where I stand now. The current regex is included below and using Expresso for testing and validating, here are the results.

01/01/09 (fails)
01/01/2009 (passes & should not)
01/01/2009 18:00 (fails)
1/1/2009 18:00 (fails)
1/01/2009 18:00 (fails)
1/1/09 18:00 (fails)
01/01/2009 18:00:00 (fails)
01/01/2009 6:00:00 pm (passes)
01/01/2009 6:00 pm (passes)

This is almost exactly what I want to happen - minus the second one. Now I insert my regular expression validator into vs2005 and run the page, and here are the results I get:

01/01/09 (fails)
01/01/2009 (passes & should not)
01/01/2009 18:00 (fails)
1/1/2009 18:00 (fails)
1/01/2009 18:00 (fails)
1/1/09 18:00 (fails)
01/01/2009 18:00:00 (fails)
01/01/2009 6:00:00 pm (passes)
01/01/2009 6:00 pm (passes)

Is there a way to ensure the user puts in a time?
^(((((0[13578])|([13578])|(1[02]))[\-/\s]?((0[1-9])|([1-9])|([1-2][0-9])|(3[01])))|((([469])|(11))[\-/\s]?((0[1-9])|([1-9])|([1-2][0-9])|(30)))|((02|2)[\-/\s]?((0[1-9])|([1-9])|([1-2][0-9]))))[\-/\s]?\d{4})(\s(((0[1-9])|([1-9])|(1[0-2]))\:([0-5][0-9])((\s)|(\:([0-5][0-9])\s))([AM|PM|am|pm]{2,2})))?$

Open in new window

please see my comment http:#23674429 and answer the questions there

> ..ensure the user puts in a time?
how do you define "time"?

Commented:
This i what a suggest.

For a better user experience i will suggest use a "Date picker" control.
Ajax control toolkit provide what you need.
http://www.asp.net/AJAX/AjaxControlToolkit/Samples/Calendar/Calendar.aspx

Also for you time i will say use 3 drop down. 1 fill out with hours, a other with minutes and a last one with "AM/PM".

That way all your user will not have to take care about the format, they just choose the information in the controls you provide. On server side, you can easily built your datetime variable from provided informations.

The ideal solution will be group all those 4 controls (date picket + drop down) into a single custom "User Control" that can be easily add and manipulate in any page just by using 1 control with your custom properties.

What you think?
Carla RomereDirector of Information Technology

Author

Commented:
ahoffman - I'm not familiar with writing regular expressions and simply started with one I found on the internet to see if it would do what I required. I didn't write the code for the page that's requesting this date and am just coming into an existing situation. I'm not even sure what you mean about using grouped matches. I did remove the question mark and that got it to the point that it required the time to be entered.

Then it was to the point that it required the date and time, but allowed times through without the milliseconds being entered. I played with it a little bit and it is now working correctly. The regex I ended up with is pasted below.

jabcoco - This page was built by someone else and has other requirements and options built into it that prevented me from using a datepicker control. But, I wholeheartedly agree that would have been a better option to use from the start.
^(((((0[13578])|([13578])|(1[02]))[\-/\s]?((0[1-9])|([1-9])|([1-2][0-9])|(3[01])))|((([469])|(11))[\-/\s]?((0[1-9])|([1-9])|([1-2][0-9])|(30)))|((02|2)[\-/\s]((0[1-9])|([1-9])|([1-2][0-9]))))[\-/\s]\d{4})(\s(((0[1-9])|([1-9])|(1[0-2]))\:([0-5][0-9])((\:([0-5][0-9])\s))([AM|PM|am|pm]{2,2})))$

Open in new window

> .. mean about using grouped matches.
anything enclosed in () is a group
any group can be back referenced unless it is written as (?: )

In your example ther a dozent of nested groups, which seems to be useless (at least to me:)
Carla RomereDirector of Information Technology

Author

Commented:
Replacing the slashes got me a step further as well as removing the question mark at the end. Thank you both for your help.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.