473,395 Members | 1,931 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,395 software developers and data experts.

RegExp Again!

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
Mar 28 '08 #1
7 1302
On 28 âÅÒ, 05:45, RN1 <r...@rediffmail.comwrote:
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"/>
--------------------------------------------------------------------------------
There is a less complex solution:
ValidationExpression="\d{2,3}"

Regards,
Mykola
http://marss.co.ua
Mar 28 '08 #2

"RN1" <rn**@rediffmail.comwrote in message
news:e4**********************************@n58g2000 hsf.googlegroups.com...
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
Check out the Expresso application. Written in dot.net and has a GUI for
developing regex. Lots of examples of expressions built in and it will
generate code in both C# and VB.Net. Been using it since dot.net v.0.

LS

Mar 28 '08 #3
RN1
On Mar 28, 7:46*pm, "Lloyd Sheen" <a...@b.cwrote:
"RN1" <r...@rediffmail.comwrote in message

news:e4**********************************@n58g2000 hsf.googlegroups.com...


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

Check out the Expresso application. *Written in dot.net and has a GUI for
developing regex. *Lots of examples of expressions built in and it will
generate code in both C# and VB.Net. *Been using it since dot.net v.0.

LS- Hide quoted text -

- Show quoted text -
Sorry to say, friends, but I don't want an alternate/easier expression
or examples. Rather I would like to UNDERSTAND the LOGIC behind why

[0-9]{2}|[0-9]{3}

evaluates 216 to False & why

([0-9]{2}|[0-9]{3})$

evaluates 216 to True?

Thanks,

Ron
Mar 29 '08 #4
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:

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

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
Mar 31 '08 #5
RN1
On Mar 31, 11:53*pm, Jesse Houwing <jesse.houw...@newsgroup.nospam>
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:
----------------------------------------------------------------------
----------
<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.

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
Mar 31 '08 #6
Hello RN1,
On Mar 31, 11:53 pm, Jesse Houwing <jesse.houw...@newsgroup.nospam>
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:

--------------------------------------------------------------------
--
----------
<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.
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
Mar 31 '08 #7
RN1
On Apr 1, 12:58*am, Jesse Houwing <jesse.houw...@newsgroup.nospam>
wrote:
Hello RN1,


On Mar 31, 11:53 pm, Jesse Houwing <jesse.houw...@newsgroup.nospam>
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:
>--------------------------------------------------------------------
--
----------
<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.
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 numbers
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
Mar 31 '08 #8

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

Similar topics

2
by: Patryk Konieczka | last post by:
Hello Here's the thing I have a database edited by some company workers editing descriptions of books in the sotre , unfortunately these workers do not have the habit of inserting a space...
5
by: Lukas Holcik | last post by:
Hi everyone! How can I simply search text for regexps (lets say <a href="(.*?)">(.*?)</a>) and save all URLs(1) and link contents(2) in a dictionary { name : URL}? In a single pass if it could....
5
by: Dr John Stockton | last post by:
ISTM that RegExps deserve a FAQ entry, with links to more detailed sources. An important question, probably not treated by many otherwise worthwhile sources, must be on feature detection of the...
6
by: Nick | last post by:
Hi, How can I check if a number exists by itself in this string by using the RegExp object? --- var mystring = "11,111,01,011"; var match = "1"; var re = new RegExp( match );
6
by: Edward | last post by:
I need to validate a text box entry, but ONLY if it is 17 characters, otherwise I have to ignore it. My regular expression for the validation is: ^(({9})()()(\d{6}))$ Can I adapt this to...
26
by: Matt Kruse | last post by:
Are there any current browsers that have Javascript support, but not RegExp support? For example, cell phone browsers, blackberrys, or other "minimal" browsers? I know that someone using Netscape...
11
by: HopfZ | last post by:
I coudn't understand some behavior of RegExp.test function. Example html code: ---------------- <html><head></head><body><script type="text/javascript"> var r = /^https?:\/\//g;...
2
by: Uldis Bojars | last post by:
Hi All, I have encountered problems with JS RegExp.exec() and can't find what is the problem. Could you help me? formRequest is a function that extracts some information from XMLHTTPRequest...
1
Atli
by: Atli | last post by:
The following small HowTo is a compilation of an original problem in getting some cookie-values through different methods of string-handling. The original Problem was posted as follows: As...
4
by: Matt | last post by:
Hello all, I have just discovered (the long way) that using a RegExp object with the 'global' flag set produces inconsistent results when its test() method is executed. I realize that 'global'...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...

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.