Solved

How to write RegularExpressionValidator for time?

Posted on 2004-09-27
10
386 Views
Last Modified: 2008-02-01
I have a textbox which let user key in time..
sample of data:
06:00 AM
9:15 PM


User only can key in the 12 hour format.
how to write the Regular Expression for such input?
Thanks!!!!
0
Comment
Question by:p931226
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 17

Expert Comment

by:AerosSaga
Comment Utility
I would just find one here there are plenty:
http://www.regexlib.com/

but if you want an editor to write RegEx download this one, its hot and best of all its free.
http://royo.is-a-geek.com/iserializable/regulator/

Regards,

Aeros

0
 

Author Comment

by:p931226
Comment Utility
I try this:
(<Time>?^(?:0?[1-9]:[0-5]|1(?=[012])\d:[0-5])\d(?:[ap]m)?)


but it can't work....
any other regular exp?
0
 
LVL 2

Expert Comment

by:raed_hasan
Comment Utility
(?<Time>^(?:0?[1-9]:[0-5]|1(?=[012])\d:[0-5])\d(?:[ap]m)?)

and you may Check this tool out http://www.sellsbrothers.com/tools/#regexd

Regards
0
 

Author Comment

by:p931226
Comment Utility
(?<Time>^(?:0?[1-9]:[0-5]|1(?=[012])\d:[0-5])\d(?:[ap]m)?)


using the above regex, show scripting error ...
dunno why??
0
 
LVL 2

Expert Comment

by:Arvaris
Comment Utility
try this

^(1|01|2|02|3|03|4|04|5|05|6|06|7|07|8|08|9|09|10|11|12{1,2}
):(([0-5]{1}[0-9]{1}\s{0,1})([AM|PM|am|pm]{2,2}))\W{0}$
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 2

Expert Comment

by:raed_hasan
Comment Utility
Try this
Regex r = new
Regex(@"(?<Time>^(?:0?[1-9]:[0-5]|1(?=[012])\d:[0-5])\d(?:[ap]m)?)",RegexOptions.IgnoreCase);

Note the brackets within quotations

Regards
0
 

Author Comment

by:p931226
Comment Utility
I try this:

^(1|01|2|02|3|03|4|04|5|05|6|06|7|07|8|08|9|09|10|11|12{1,2}
):(([0-5]{1}[0-9]{1}\s{0,1})([AM|PM|am|pm]{2,2}))\W{0}$

it works, but how to ignore the space infront of the time?
if I key in the time like
12:00AM
it works..
but when I key in
"space"12:00AM
12:00"Space"AM
it can't work...
how to ignore the space????




0
 

Author Comment

by:p931226
Comment Utility
Lastly I use this:
^((1|01|2|02|3|03|4|04|5|05|6|06|7|07|8|08|9|09|10|11|12{1,2}):(([0-5]{1}[0-9]{1}\s{0,1})(\s+)([AM|PM|am|pm]{2,2}))\W{0}$)|((1|01|2|02|3|03|4|04|5|05|6|06|7|07|8|08|9|09|10|11|12{1,2}):(([0-5]{1}[0-9]{1}\s{0,1})([AM|PM|am|pm]{2,2}))\W{0}$)|(\s+(1|01|2|02|3|03|4|04|5|05|6|06|7|07|8|08|9|09|10|11|12{1,2}):(([0-5]{1}[0-9]{1}\s{0,1})([AM|PM|am|pm]{2,2}))\W{0}$)|(\s+(1|01|2|02|3|03|4|04|5|05|6|06|7|07|8|08|9|09|10|11|12{1,2}):(([0-5]{1}[0-9]{1}\s{0,1})(\s+)([AM|PM|am|pm]{2,2}))\W{0}$)

in order to get
[12:00pm][ 12:00pm][12:00   pm] matched....

any other way shorter than this?
thanks.
0
 
LVL 2

Accepted Solution

by:
Arvaris earned 80 total points
Comment Utility
Just add where all there spaces could be together, since it is, therefore you could get

[ 12:00 pm]

^(\s*(1|01|2|02|3|03|4|04|5|05|6|06|7|07|8|08|9|09|10|11|12{1,2}):(([0-5]{1}[0-9]{1}\s{0,1})([AM|PM|am|pm]{2,2}))\W{0}$)

I took out the (\s+) because you already have \s{0,1} to allow for a single space (you could also use \s? because ? means 0 or 1), also I changed the \s+ at the beginning to \s* because + means 1 or more, * means 0 or more.  If you only want an optional 1 space at the beginning, just change it to \s{0,1} like the other one.

Let me know if this works.
0
 

Author Comment

by:p931226
Comment Utility
Hi Arvaris,
your Regex works!!
bcoz I would like to allow user to key in something like
[     12:00                 PM]

so I change the \s{0,1} to \s*, so it become like this:

^(\s*(1|01|2|02|3|03|4|04|5|05|6|06|7|07|8|08|9|09|10|11|12{1,2}):(([0-5]{1}[0-9]{1}\s*)([AM|PM|am|pm]{2,2}))\W{0}$)


Anyway, thanks a lot everyone.

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now