Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to write RegularExpressionValidator for time?

Posted on 2004-09-27
10
Medium Priority
?
393 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 320 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

618 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