• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 394
  • Last Modified:

How to write RegularExpressionValidator for time?

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
p931226
Asked:
p931226
  • 5
  • 2
  • 2
  • +1
1 Solution
 
AerosSagaCommented:
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
 
p931226Author Commented:
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
 
raed_hasanCommented:
(?<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.

 
p931226Author Commented:
(?<Time>^(?:0?[1-9]:[0-5]|1(?=[012])\d:[0-5])\d(?:[ap]m)?)


using the above regex, show scripting error ...
dunno why??
0
 
ArvarisCommented:
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
 
raed_hasanCommented:
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
 
p931226Author Commented:
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
 
p931226Author Commented:
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
 
ArvarisCommented:
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
 
p931226Author Commented:
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 5
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now