What's wrong with RegularExpressionValidator?

C

clintonG

Two regular expression questions in a row -- we're on a roll here ;-)

I have this...

<asp:RegularExpressionValidator
ID="Validator1" runat="server"
ControlToValidate="TextBox1"
EnableViewState="False"
ValidationExpression="^[-\w\d\.]$"
ErrorMessage='Disallowed Data Entry. Review Usage Notes.'
Display="Dynamic"
Font-Bold="True" />

It is supposed to allow any alphanumeric character, underscore or hyphen
delineated with a dot character. When ommiting the ^ and $ characters the
expression works fine in this RegEx tool [1] that runs in the webpage but
the expression won't work in my application no matter what I type into the
textbox. This is very puzzeling. Any insight here?

<%= Clinton Gallagher
NET csgallagher AT metromilwaukee.com
URL http://clintongallagher.metromilwaukee.com/

[1] http://www.sweeting.org/mark/html/revalid.php
 
D

Damien

Two regular expression questions in a row -- we're on a roll here ;-)

I have this...

<asp:RegularExpressionValidator
ID="Validator1" runat="server"
ControlToValidate="TextBox1"
EnableViewState="False"
ValidationExpression="^[-\w\d\.]$"
ErrorMessage='Disallowed Data Entry. Review Usage Notes.'
Display="Dynamic"
Font-Bold="True" />

It is supposed to allow any alphanumeric character, underscore or hyphen
delineated with a dot character. When ommiting the ^ and $ characters the
expression works fine in this RegEx tool [1] that runs in the webpage but
the expression won't work in my application no matter what I type into the
textbox. This is very puzzeling. Any insight here?

<%= Clinton Gallagher
NET csgallagher AT metromilwaukee.com
URLhttp://clintongallagher.metromilwaukee.com/

[1]http://www.sweeting.org/mark/html/revalid.php

Hi Clinton,

I'm a bit unclear on what your expression is meant to allow/disallow.
Could you post maybe 5 examples of valid values, and as many invalid
values as neccessary to demonstrate your rules.

Damien
 
C

clintonG

Damien said:
Two regular expression questions in a row -- we're on a roll here ;-)

I have this...

<asp:RegularExpressionValidator
ID="Validator1" runat="server"
ControlToValidate="TextBox1"
EnableViewState="False"
ValidationExpression="^[-\w\d\.]$"
ErrorMessage='Disallowed Data Entry. Review Usage Notes.'
Display="Dynamic"
Font-Bold="True" />

It is supposed to allow any alphanumeric character, underscore or hyphen
delineated with a dot character. When ommiting the ^ and $ characters the
expression works fine in this RegEx tool [1] that runs in the webpage but
the expression won't work in my application no matter what I type into
the
textbox. This is very puzzeling. Any insight here?

<%= Clinton Gallagher
NET csgallagher AT metromilwaukee.com
URLhttp://clintongallagher.metromilwaukee.com/

[1]http://www.sweeting.org/mark/html/revalid.php

Hi Clinton,

I'm a bit unclear on what your expression is meant to allow/disallow.
Could you post maybe 5 examples of valid values, and as many invalid
values as neccessary to demonstrate your rules.

Damien

Oops. I took it for granted thinking this a simple expression everybody
could read noting
the expression itself functions when tested in other applications. I just
seem to have problems using it in a page which really puzzles me because the
same type of validator works elsewhere in the same page.

Anyhow... an example containing all allowable characters follows the **
delineator

** web_1.a-b.com

Try it out here...
http://www.sweeting.org:80/mark/html/revalid.php

<%= Clinton Gallagher
 
D

Damien

Two regular expression questions in a row -- we're on a roll here ;-)
I have this...
<asp:RegularExpressionValidator
ID="Validator1" runat="server"
ControlToValidate="TextBox1"
EnableViewState="False"
ValidationExpression="^[-\w\d\.]$"
ErrorMessage='Disallowed Data Entry. Review Usage Notes.'
Display="Dynamic"
Font-Bold="True" />
It is supposed to allow any alphanumeric character, underscore or hyphen
delineated with a dot character. When ommiting the ^ and $ characters the
expression works fine in this RegEx tool [1] that runs in the webpage but
the expression won't work in my application no matter what I type into
the
textbox. This is very puzzeling. Any insight here?
<%= Clinton Gallagher
NET csgallagher AT metromilwaukee.com
URLhttp://clintongallagher.metromilwaukee.com/
[1]http://www.sweeting.org/mark/html/revalid.php
Hi Clinton,
I'm a bit unclear on what your expression is meant to allow/disallow.
Could you post maybe 5 examples of valid values, and as many invalid
values as neccessary to demonstrate your rules.

Oops. I took it for granted thinking this a simple expression everybody
could read noting
the expression itself functions when tested in other applications. I just
seem to have problems using it in a page which really puzzles me because the
same type of validator works elsewhere in the same page.

Anyhow... an example containing all allowable characters follows the **
delineator

** web_1.a-b.com

Try it out here...http://www.sweeting.org:80/mark/html/revalid.php

<%= Clinton Gallagher

Yes, but the regular expression you've given will match only a single
character. When you take out the ^ and $, you'll get 13 distinct
matches, whereas presumably you want 1 match for the entire string. I
think you've said that you want it dot delimited (i.e. not to start or
end with a dot, in which case the following may suit your purposes (it
assumes that the string must be at least 2 characters long):

^[-\w\d][-\w\d\.]*[-\w\d]$

If you *require* it to always contain at least 1 dot, then something
like this would do:

^[-\w\d][-\w\d]*\.[-\w\d\.]*[-\w\d]$

This is why I asked for some valid and invalid examples so I could
steer towards an expression that would work well for your purposes -
it's difficult for me to write REs when I'm not given a spec :)

Damien
 
C

clintonG

<snip />

^[-\w\d][-\w\d]*\.[-\w\d\.]*[-\w\d]$

Oh gee thanks, and now all of a sudden that validation is just fine? I only
got one thing to say...
I really need to find another line of work ;-) but that was really helpful
so one more thing please.
The string is a domain name without protocol handler. How do I disallow
starting the entry with a hyphen or the underscore while allowing those
characters inline?

<%= Clinton
 
C

clintonG

<snip />

Okay, I forgot again and you're right, clarity of intent is critical for
these expressions as I've once again discovered after breaking what you've
taken your time to help express...

The string is a domain name with no protocol handler, expressed in all lower
case characters with no delimiters but multiple dot character, a single
hyphen between characters, or a single underscore between characters a-z and
0-9 but will not allow the domain name to start with any character except
a-z or 0-9 nor end with any character except a-z. How's that?

I'm delving into RegExLib...

<%= Clinton
 

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,809
Latest member
moe77

Latest Threads

Top