Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / March 2008

Tip: Looking for answers? Try searching our database.

Regex help

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Leon Mayne - 03 Mar 2008 11:36 GMT
I'm trying to add a regex validator to a webform textbox to validate time
format. The field is optional, so I'm trying to get a regex which will
validate empty, or the format
[00-23]:[00-59]
Is anyone here good with regular expressions and can point me to a pattern
that will do the above?
Alexey Smirnov - 03 Mar 2008 12:30 GMT
> I'm trying to add a regex validator to a webform textbox to validate time
> format. The field is optional, so I'm trying to get a regex which will
> validate empty, or the format
> [00-23]:[00-59]
> Is anyone here good with regular expressions and can point me to a pattern
> that will do the above?

try

\[\d\d-\d\d\]:\[\d\d-\d\d\]
Leon Mayne - 03 Mar 2008 13:23 GMT
> \[\d\d-\d\d\]:\[\d\d-\d\d\]

Hmm, won't that show 99:99 as valid, and an empty string as invalid?
Alexey Smirnov - 03 Mar 2008 13:56 GMT
> > \[\d\d-\d\d\]:\[\d\d-\d\d\]
>
> Hmm, won't that show 99:99 as valid,

you're right, but then you should extend your validation expression

e.g.

\[[0|1|2][0|1|2|3]-[0|1|2][0|1|2|3]]......

to match

[00-01]...,
[22-23]...

but not

[24-25]...,
etc

> and an empty string as invalid?

RegularExpressionValidator won't fire when input is empty. When you
want to disallow an empty string, you should add the
RequiredFieldValidator Control.
Leon Mayne - 03 Mar 2008 14:06 GMT
> you're right, but then you should extend your validation expression

Yes, I think I've compressed my original one down now:
([0-1][0-9]|2[0-3]):([0-5][0-9])
Does that look right?

> RegularExpressionValidator won't fire when input is empty

True, I forgot about that.

Thanks for your help
Alexey Smirnov - 03 Mar 2008 20:19 GMT
> > you're right, but then you should extend your validation expression
>
> Yes, I think I've compressed my original one down now:
> ([0-1][0-9]|2[0-3]):([0-5][0-9])
> Does that look right?

No.

The logic is very simple. The brackets [] denote a number range.

[0|1|2] indicates that we are looking for 0, 1, or 2
[0|1|2|3] indicates that we are looking for 0, 1, 2, or 3

so, [0|1|2][0|1|2|3] will validate 00, 01, 02 .. 23, but not 24, 25
etc.

So, to validate "[00-23]:[00-59]" you would need something like the
following string

\[[0|1|2][0|1|2|3]-[0|1|2][0|1|2|3]\]:\[[0|1|2|3|4|5][0|1|2|3|4|5|6|7|
8|9]-[0|1|2|3|4|5][0|1|2|3|4|5|6|7|8|9]\]

Hope this helps
Leon Mayne - 04 Mar 2008 08:44 GMT
>> Yes, I think I've compressed my original one down now:
>> ([0-1][0-9]|2[0-3]):([0-5][0-9])
[quoted text clipped - 12 lines]
> \[[0|1|2][0|1|2|3]-[0|1|2][0|1|2|3]\]:\[[0|1|2|3|4|5][0|1|2|3|4|5|6|7|
> 8|9]-[0|1|2|3|4|5][0|1|2|3|4|5|6|7|8|9]\]

What's the difference between [0|1|2|3|4|5|6|7|8|9] and [0-9]?
Alexey Smirnov - 04 Mar 2008 09:42 GMT
> >> Yes, I think I've compressed my original one down now:
> >> ([0-1][0-9]|2[0-3]):([0-5][0-9])
[quoted text clipped - 16 lines]
>
> - Show quoted text -

Hi Leon

Sorry I think you're right and I did a mistake in my answer. The
syntax for the regular-expression validation controls is a little bit
different than on the server and so, it's always better to validate
each expression using a validator control (I thought 0-9 will not work
there). Please refer to http://msdn2.microsoft.com/en-us/library/ae5bf541.aspx.
So, if you want to validate a regular 24hr time input string then you
expression will work.

Note, "[0-9]" equivalent to "\d"
Leon Mayne - 04 Mar 2008 11:29 GMT
> Note, "[0-9]" equivalent to "\d"

Good point, so it could be:

([0-1]\d|2[0-3]):([0-5]\d)
Jesse Houwing - 04 Mar 2008 13:22 GMT
Hello Alexey,

>> "Alexey Smirnov" <alexey.smir...@gmail.com> wrote in message
>>
[quoted text clipped - 32 lines]
> expression will work.
> Note, "[0-9]" equivalent to "\d"

As a side note:
[0123456789] equals [0-9]
but [0|1|2|3|4|5|6|7|8|9|0] equals [0-9|] you should not use the | within
a range.

Another note, the clientside and serverside validations are almost equal.
A few specifics in substracting ranges from an existing group and look behinds
are all that differentiate them. You can find the exact info on http://www.regular-expressions.info/refflavors.html

--
Jesse Houwing
jesse.houwing at sogeti.nl
Leon Mayne - 03 Mar 2008 13:52 GMT
> I'm trying to add a regex validator to a webform textbox to validate time
> format. The field is optional, so I'm trying to get a regex which will
> validate empty, or the format
> [00-23]:[00-59]
> Is anyone here good with regular expressions and can point me to a pattern
> that will do the above?

Oh wait, I think I've got it.
Would:
(0[0-9]|1[0-9]|2[0-3]):(0[0-9]|[0-5][0-9])
Work?

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.