RegExpression Help

J

Jim in Arizona

I set up a regular expression validator on my web form to make sure
users were putting in correct time format but I can't seem to get it right.

The problem is that I need them to input a time based on the 12 hour
clock. This regular expression works fine:

^([0-1][0-2]):([0-5][0-9])$

However, this does not include AM or PM designators and this is where I
can't seem to find the proper expression. I have tried this:

^([0-1][0-2]):([0-5][0-9]):b([AM]|[am]|[PM]|[pm])$

That one doesn't work. I even tried making it as simple as possible just
for testing purposes, like so:

^([0-1][0-2]):([0-5][0-9])([AM])$
or
^([0-1][0-2]):([0-5][0-9])AM$

But, when I put in a time like 12:20AM or 12:20 AM, the validator is
triggered.

I'm hoping to avoid using a 24 hour clock as this would just cause
frustration for our secretaries.

TIA,
Jim
 
G

Guest

I set up a regular expression validator on my web form to make sure
users were putting in correct time format but I can't seem to get it right.

The problem is that I need them to input a time based on the 12 hour
clock. This regular expression works fine:

^([0-1][0-2]):([0-5][0-9])$

However, this does not include AM or PM designators and this is where I
can't seem to find the proper expression. I have tried this:

^([0-1][0-2]):([0-5][0-9]):b([AM]|[am]|[PM]|[pm])$

That one doesn't work. I even tried making it as simple as possible just
for testing purposes, like so:

^([0-1][0-2]):([0-5][0-9])([AM])$
or
^([0-1][0-2]):([0-5][0-9])AM$

But, when I put in a time like 12:20AM or 12:20 AM, the validator is
triggered.

I'm hoping to avoid using a 24 hour clock as this would just cause
frustration for our secretaries.

TIA,
Jim

Jim, you don't need brackets (used to create a set of characters) and
because of single space you have to use a "\s"

This will work

^([0-1][0-2]):([0-5][0-9]\s?AM|am|PM|pm)$
 
J

Jim in Arizona

Jim, you don't need brackets (used to create a set of characters) and
because of single space you have to use a "\s"

This will work

^([0-1][0-2]):([0-5][0-9]\s?AM|am|PM|pm)$


I'm pretty new at regular expressions being that this is my first time
ever trying to use them. I was going off the msdn article at
http://msdn2.microsoft.com/en-us/library/2k3te2cs(VS.80).aspx and the :b
was supposed to be a single space. Of course, I was using it wrong.

Thanks for your help. I'll be able to move forward with my project now.
 
C

Cowboy \(Gregory A. Beamer\)

^(((([1-9])|(([0][0-9])|([1][0-2]))):[0-5][0-9])\s?(AM|PM)?)|(([0-9])|([0-1][0-9])|([2][0-3])):([0-5][0-9])$

Tests the following as true

12 hour time
1:00
12:59
1:01 AM
1:01 PM
12:59 AM
12:59 PM

24 hour time
00:01 - 12:01 AM
23:59 - 11:59 PM

Will fail on AM/PM on any 24 hour designation (anything 13:00 and above).
Have not fully tested all varieties.

You know you can also test valid times like this:

public static bool isValidTime(string time, ref DateTime dt)
{
return DateTime.TryParse(time, out dt);
}
 
G

Guest

Jim, you don't need brackets (used to create a set of characters) and
because of single space you have to use a "\s"
This will work
^([0-1][0-2]):([0-5][0-9]\s?AM|am|PM|pm)$

I'm pretty new at regular expressions being that this is my first time
ever trying to use them. I was going off the msdn article athttp://msdn2.microsoft.com/en-us/library/2k3te2cs(VS.80).aspxand the :b
was supposed to be a single space. Of course, I was using it wrong.

Jim, that article is about a regular expressions used in the Visual
Studio's "Find and Replace Window" :)

You need to look at .NET Regular Expressions,

for example here
http://msdn2.microsoft.com/en-us/library/20bw873z(VS.71).aspx
 
J

Jim in Arizona

Alexey said:
Jim, you don't need brackets (used to create a set of characters) and
because of single space you have to use a "\s"
This will work
^([0-1][0-2]):([0-5][0-9]\s?AM|am|PM|pm)$
I'm pretty new at regular expressions being that this is my first time
ever trying to use them. I was going off the msdn article athttp://msdn2.microsoft.com/en-us/library/2k3te2cs(VS.80).aspxand the :b
was supposed to be a single space. Of course, I was using it wrong.

Jim, that article is about a regular expressions used in the Visual
Studio's "Find and Replace Window" :)

You need to look at .NET Regular Expressions,

for example here
http://msdn2.microsoft.com/en-us/library/20bw873z(VS.71).aspx

Well. That makes a world of difference, huh?
Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Help with code 0
Machine Learning.. Endless Struggle 3
Help with my responsive home page 2
Code help please 4
Pygame animation help!!!! 0
Minimum Total Difficulty 0
Help with Loop 0
Need help for javascript code 3

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top