Solved

How to write RegularExpressionValidator for time?

Posted on 2004-09-27
10
390 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

Suggested Solutions

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project

738 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