Carla Romere
asked on
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.
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})))?$
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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})))?$
ASKER
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.
The user must enter their datetime as 02/01/2009 6:00 am/pm (AM/PM) or the page times out.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
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})))?$
please see my comment http:#23674429 and answer the questions there
> ..ensure the user puts in a time?
how do you define "time"?
> ..ensure the user puts in a time?
how do you define "time"?
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?
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?
ASKER
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.
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})))$
> .. 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:)
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:)
ASKER
Replacing the slashes got me a step further as well as removing the question mark at the end. Thank you both for your help.
ASKER