R
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:
--------------------------------------------------------------------------------
<asp:TextBox ID="txt1" runat="server"/>
<asp:RegularExpressionValidator ID="regexp1" 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.
Thanks,
Ron
digit or a 3-digit whole number in a TextBox. This is how I framed the
ValidationExpression in the RegularExpressionValidator:
--------------------------------------------------------------------------------
<asp:TextBox ID="txt1" runat="server"/>
<asp:RegularExpressionValidator ID="regexp1" 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.
Thanks,
Ron