[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 345
  • Last Modified:

Regular Expression needed

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

0
Carla Romere
Asked:
Carla Romere
2 Solutions
 
jmwheelerCommented:
Since your using C# you need to replace \ (single backslash) with \\ (double backslash)
0
 
Carla RomereDirector of Information TechnologyAuthor Commented:
I replaced all the single backslashes with double backslashes and it still isn't working correctly. Calls it invalid when it's not.
0
 
Ionut A. TudorPHP ProgrammerCommented:
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

0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
Carla RomereDirector of Information TechnologyAuthor 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
 
ahoffmannCommented:
(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?
0
 
Carla RomereDirector of Information TechnologyAuthor 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

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

> ..ensure the user puts in a time?
how do you define "time"?
0
 
jabcocoCommented:
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?
0
 
Carla RomereDirector of Information TechnologyAuthor 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

0
 
ahoffmannCommented:
> .. 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:)
0
 
Carla RomereDirector of Information TechnologyAuthor 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.
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now