473,402 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

RegExpression Help

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
Apr 2 '07 #1
5 1180
On Apr 2, 7:03 pm, Jim in Arizona <tiltow...@hotmail.comwrote:
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)$

Apr 2 '07 #2
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/lib...cs(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.
Apr 2 '07 #3
^(((([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);
}

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

*********************************************
Think outside the box!
*********************************************
"Jim in Arizona" <ti*******@hotmail.comwrote in message
news:O6**************@TK2MSFTNGP02.phx.gbl...
>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
Apr 2 '07 #4
On Apr 2, 9:46 pm, Jim in Arizona <tiltow...@hotmail.comwrote:
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/lib...3z(VS.71).aspx

Apr 2 '07 #5
Alexey Smirnov wrote:
On Apr 2, 9:46 pm, Jim in Arizona <tiltow...@hotmail.comwrote:
>>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/lib...3z(VS.71).aspx
Well. That makes a world of difference, huh?
Thanks again.
Apr 2 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
3
by: H Branyan | last post by:
I need to format a textbox value a user enters. The user will enter 13 characters, and then I have to format them, preferably in the onBlur event, to look like this: XXXX-XX-XXX-XXXX I found...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
3
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With...
7
by: Corepaul | last post by:
Missing Help Files When I enter "recordset" as the keyword and search the Visual Basic Help index, I get many topics of interest in the resulting list. But there isn't any information available...
5
by: Steve | last post by:
I have written a help file (chm) for a DLL and referenced it using Help.ShowHelp My expectation is that a developer using my DLL would be able to access this help file during his development time...
8
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.