Re: RegExp Again!
am 31.03.2008 21:17:19 von rn5a
On Mar 31, 11:53=A0pm, Jesse Houwing
wrote:
> Hello RN1,
>
>
>
>
>
> > Using Regular Expression, I want to ensure that users enter either a
> > 2- digit or a 3-digit whole number in a TextBox. This is how I framed
> > the ValidationExpression in the RegularExpressionValidator:
>
> > ------------------------------------------------------------ ----------
> > ----------
> >
> >
"
> > Display=3D"dynamic" ErrorMessage=3D"Invalid Text"
> > ValidationExpression=3D"[0-9]{3}|[0-9]{2}" runat=3D"server"/>
> > ------------------------------------------------------------ ----------
> > ----------
> > Suppose a user enters 16 & 216 in the TextBox. As expected, both the
> > numbers return True but if I just reverse the ValidationExpression
> > i.e. change the ValidationExpression from
>
> > [0-9]{3}|[0-9]{2}
>
> > to
>
> > [0-9]{2}|[0-9]{3}
>
> > & then input 16 & 216 in the TextBox, 16 correctly evaluates to True
> > but 216 strangely evaluates to False!
>
> > After a plethora of trial & error methods, I realized that if the
> > second ValidationExpression (the one which evaluates 216 to False) is
> > enclosed in brackets & a $ sign is appended at the end of the
> > expression like this
>
> > ([0-9]{2}|[0-9]{3})$
>
> > then 216 correctly evaluates to True (& so does 16).
>
> > What I couldn't figure out is the logic behind the expression when it
> > is wrapped in brackets & a $ sign is appended at the end! Can someone
> > please explain me this? What more work does the edited
> > ValidationExpression do to make 216 evaluate to True?
>
> > I am aware that $ means the end of a string.
>
> To start with, [0-9]{2,3} would be the sorter variant of your original exp=
ression,
> but to explain why this isn't working as expected: The RegularExpressionVa=
lidator
> puts a ^ and a $ around every expression, so the expression under test is
> actually:
>
> ^[0-9]{2}|[0-9]{3}$
>
> Which should be read as:
>
> ^[0-9]{2} or [0-9]{3}$
>
> So: any string starting with 2 numbers or any string ending in two numbers=
..
>
> ([0-9]{2}|[0-9]{3})
>
> Adding () solves this because that in turn expands to
>
> ^([0-9]{2}|[0-9]{3})$ or ^[0-9]{2}$|^[0-9]{3}$
>
> The best solution is to make sure all your validation expressions are eith=
er
> in (..), or ^..$.
>
> --
> Jesse Houwing
> jesse.houwing at sogeti.nl- Hide quoted text -
>
> - Show quoted text -
>> So: any string starting with 2 numbers or any string ending >> in two num=
bers
Shouldn't that be "so any string starting with 2 numbers or any string
ending in *three* numbers"?
Ron
Re: RegExp Again!
am 31.03.2008 21:58:44 von Jesse Houwing
Hello RN1,
> On Mar 31, 11:53 pm, Jesse Houwing
> wrote:
>
>> Hello RN1,
>>
>>> Using Regular Expression, I want to ensure that users enter either a
>>> 2- digit or a 3-digit whole number in a TextBox. This is how I
>>> framed the ValidationExpression in the RegularExpressionValidator:
>>>
>>> ------------------------------------------------------------ --------
>>> --
>>> ----------
>>>
>>>
>>> ControlToValidate="txt1"
>>> Display="dynamic" ErrorMessage="Invalid Text"
>>> ValidationExpression="[0-9]{3}|[0-9]{2}" runat="server"/>
>>> ------------------------------------------------------------ --------
>>> --
>>> ----------
>>> Suppose a user enters 16 & 216 in the TextBox. As expected, both the
>>> numbers return True but if I just reverse the ValidationExpression
>>> i.e. change the ValidationExpression from
>>> [0-9]{3}|[0-9]{2}
>>>
>>> to
>>>
>>> [0-9]{2}|[0-9]{3}
>>>
>>> & then input 16 & 216 in the TextBox, 16 correctly evaluates to True
>>> but 216 strangely evaluates to False!
>>>
>>> After a plethora of trial & error methods, I realized that if the
>>> second ValidationExpression (the one which evaluates 216 to False)
>>> is enclosed in brackets & a $ sign is appended at the end of the
>>> expression like this
>>>
>>> ([0-9]{2}|[0-9]{3})$
>>>
>>> then 216 correctly evaluates to True (& so does 16).
>>>
>>> What I couldn't figure out is the logic behind the expression when
>>> it is wrapped in brackets & a $ sign is appended at the end! Can
>>> someone please explain me this? What more work does the edited
>>> ValidationExpression do to make 216 evaluate to True?
>>>
>>> I am aware that $ means the end of a string.
>>>
>> To start with, [0-9]{2,3} would be the sorter variant of your
>> original expression, but to explain why this isn't working as
>> expected: The RegularExpressionValidator puts a ^ and a $ around
>> every expression, so the expression under test is actually:
>>
>> ^[0-9]{2}|[0-9]{3}$
>>
>> Which should be read as:
>>
>> ^[0-9]{2} or [0-9]{3}$
>>
>> So: any string starting with 2 numbers or any string ending in two
>> numbers.
>>
>> ([0-9]{2}|[0-9]{3})
>>
>> Adding () solves this because that in turn expands to
>>
>> ^([0-9]{2}|[0-9]{3})$ or ^[0-9]{2}$|^[0-9]{3}$
>>
>> The best solution is to make sure all your validation expressions are
>> either in (..), or ^..$.
>>
>> --
>> Jesse Houwing
>> jesse.houwing at sogeti.nl- Hide quoted text -
>> - Show quoted text -
>>
>>> So: any string starting with 2 numbers or any string ending >> in
>>> two numbers
>>>
> Shouldn't that be "so any string starting with 2 numbers or any string
> ending in *three* numbers"?
Ron,
Thank you, you're completely right.
--
Jesse Houwing
jesse.houwing at sogeti.nl
Re: RegExp Again!
am 31.03.2008 22:37:14 von rn5a
On Apr 1, 12:58=A0am, Jesse Houwing
wrote:
> Hello RN1,
>
>
>
>
>
> > On Mar 31, 11:53 pm, Jesse Houwing
> > wrote:
>
> >> Hello RN1,
>
> >>> Using Regular Expression, I want to ensure that users enter either a
> >>> 2- digit or a 3-digit whole number in a TextBox. This is how I
> >>> framed the ValidationExpression in the RegularExpressionValidator:
>
> >>> ------------------------------------------------------------ --------
> >>> --
> >>> ----------
> >>>
> >>>
> >>> ControlToValidate=3D"txt1"
> >>> Display=3D"dynamic" ErrorMessage=3D"Invalid Text"
> >>> ValidationExpression=3D"[0-9]{3}|[0-9]{2}" runat=3D"server"/>
> >>> ------------------------------------------------------------ --------
> >>> --
> >>> ----------
> >>> Suppose a user enters 16 & 216 in the TextBox. As expected, both the
> >>> numbers return True but if I just reverse the ValidationExpression
> >>> i.e. change the ValidationExpression from
> >>> [0-9]{3}|[0-9]{2}
>
> >>> to
>
> >>> [0-9]{2}|[0-9]{3}
>
> >>> & then input 16 & 216 in the TextBox, 16 correctly evaluates to True
> >>> but 216 strangely evaluates to False!
>
> >>> After a plethora of trial & error methods, I realized that if the
> >>> second ValidationExpression (the one which evaluates 216 to False)
> >>> is enclosed in brackets & a $ sign is appended at the end of the
> >>> expression like this
>
> >>> ([0-9]{2}|[0-9]{3})$
>
> >>> then 216 correctly evaluates to True (& so does 16).
>
> >>> What I couldn't figure out is the logic behind the expression when
> >>> it is wrapped in brackets & a $ sign is appended at the end! Can
> >>> someone please explain me this? What more work does the edited
> >>> ValidationExpression do to make 216 evaluate to True?
>
> >>> I am aware that $ means the end of a string.
>
> >> To start with, [0-9]{2,3} would be the sorter variant of your
> >> original expression, but to explain why this isn't working as
> >> expected: The RegularExpressionValidator puts a ^ and a $ around
> >> every expression, so the expression under test is actually:
>
> >> ^[0-9]{2}|[0-9]{3}$
>
> >> Which should be read as:
>
> >> ^[0-9]{2} or [0-9]{3}$
>
> >> So: any string starting with 2 numbers or any string ending in two
> >> numbers.
>
> >> ([0-9]{2}|[0-9]{3})
>
> >> Adding () solves this because that in turn expands to
>
> >> ^([0-9]{2}|[0-9]{3})$ or ^[0-9]{2}$|^[0-9]{3}$
>
> >> The best solution is to make sure all your validation expressions are
> >> either in (..), or ^..$.
>
> >> --
> >> Jesse Houwing
> >> jesse.houwing at sogeti.nl- Hide quoted text -
> >> - Show quoted text -
>
> >>> So: any string starting with 2 numbers or any string ending >> in
> >>> two numbers
>
> > Shouldn't that be "so any string starting with 2 numbers or any string
> > ending in *three* numbers"?
>
> Ron,
>
> Thank you, you're completely right.
>
> --
> Jesse Houwing
> jesse.houwing at sogeti.nl- Hide quoted text -
>
> - Show quoted text -
>> Which should be read as:
>> ^[0-9]{2} or [0-9]{3}$
>> So: any string starting with 2 numbers or any string ending in three numb=
ers
As you have pointed out, any string starting with two numbers or any
string ending with three numbers will evaluate to True. Now 16 is a
string starting with two numbers & hence evaluates to
True.....fine....but 216 is a string ending with three numbers; so why
does it evaluate to False?
This is getting a bit confusing....
Ron