Regex help

  • Thread starter Thread starter Leon Mayne
  • Start date Start date
L

Leon Mayne

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?
 
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 said:
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?
 
\[\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.
 
Alexey Smirnov said:
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
 
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
 
Alexey Smirnov said:
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]\]

What's the difference between [0|1|2|3|4|5|6|7|8|9] and [0-9]?
 
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]\]

What's the difference between [0|1|2|3|4|5|6|7|8|9] and [0-9]?- Hide quoted text -

- 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"
 
Hello Alexey,
m...
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]\]
What's the difference between [0|1|2|3|4|5|6|7|8|9] and [0-9]?- Hide
quoted text -

- 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"

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
 
Back
Top