Solved

How to write RegularExpressionValidator for time?

Posted on 2004-09-27
10
391 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12159315
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
ID: 12159456
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
ID: 12159620
(?<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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:p931226
ID: 12159721
(?<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
ID: 12160611
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
 
LVL 2

Expert Comment

by:raed_hasan
ID: 12162164
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
ID: 12165639
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
ID: 12166605
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
ID: 12169760
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
ID: 12175948
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

719 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