Regular Expression Weirdness

G

George Durzi

If I have a regexvalidator in a webform with a validation expression of
^[+]?[0-9]\d* and I put the text 2d in a text box being validated by this
validator, the validator fires.

This regex is a simple one that only allows the user to enter integer
values.

However, if I do this on the server side like this

Regex rx = new Regex(@"^[+]?[0-9]\d*");
Match mt = rx.Match("2d");

I get success!!!! why?? is there an option I should be setting?
 
K

Karl

I think the regularexpressionvalidator automatically puts ^ at the start and
$ at the end...2d DOES match ^[+]?[0-9]\d* but it DOES NOT match
^[+]?[0-9]\d*$ (note the extra $ at the end). That's the difference
between the two.

Also, [0-9]\d* can be rewritten as \d+

Karl
 
G

George Durzi

Karl,
Thank you again, that worked perfectly.

And thanks for the regex refining tip :)

Karl said:
I think the regularexpressionvalidator automatically puts ^ at the start and
$ at the end...2d DOES match ^[+]?[0-9]\d* but it DOES NOT match
^[+]?[0-9]\d*$ (note the extra $ at the end). That's the difference
between the two.

Also, [0-9]\d* can be rewritten as \d+

Karl

George Durzi said:
If I have a regexvalidator in a webform with a validation expression of
^[+]?[0-9]\d* and I put the text 2d in a text box being validated by this
validator, the validator fires.

This regex is a simple one that only allows the user to enter integer
values.

However, if I do this on the server side like this

Regex rx = new Regex(@"^[+]?[0-9]\d*");
Match mt = rx.Match("2d");

I get success!!!! why?? is there an option I should be setting?
 
S

S. Justin Gengo

George,

Glad you got it working. (Good eyes Karl!)

Just a note: You could also use a CompareValidator instead of the
RegularExpressionValidator. If you leave the CompareValidator's
ControlToCompare property blank and set it's Operator property to
DataTypeCheck you may then set it's Type property to check if a value
entered is a String, Integer, Double, Date, or Currency.

While I love that I can do almost anything with the regular expression
validator I find simple data type checks much easier to set up with the
CompareValidator.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
George Durzi said:
Karl,
Thank you again, that worked perfectly.

And thanks for the regex refining tip :)

Karl said:
I think the regularexpressionvalidator automatically puts ^ at the start and
$ at the end...2d DOES match ^[+]?[0-9]\d* but it DOES NOT match
^[+]?[0-9]\d*$ (note the extra $ at the end). That's the difference
between the two.

Also, [0-9]\d* can be rewritten as \d+

Karl

George Durzi said:
If I have a regexvalidator in a webform with a validation expression of
^[+]?[0-9]\d* and I put the text 2d in a text box being validated by this
validator, the validator fires.

This regex is a simple one that only allows the user to enter integer
values.

However, if I do this on the server side like this

Regex rx = new Regex(@"^[+]?[0-9]\d*");
Match mt = rx.Match("2d");

I get success!!!! why?? is there an option I should be setting?
 
G

George Durzi

That is a great tip!!! Thank you much! The regularexpressionvalidators do
seem like a little overkill for simple type checks

S. Justin Gengo said:
George,

Glad you got it working. (Good eyes Karl!)

Just a note: You could also use a CompareValidator instead of the
RegularExpressionValidator. If you leave the CompareValidator's
ControlToCompare property blank and set it's Operator property to
DataTypeCheck you may then set it's Type property to check if a value
entered is a String, Integer, Double, Date, or Currency.

While I love that I can do almost anything with the regular expression
validator I find simple data type checks much easier to set up with the
CompareValidator.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
George Durzi said:
Karl,
Thank you again, that worked perfectly.

And thanks for the regex refining tip :)

Karl said:
I think the regularexpressionvalidator automatically puts ^ at the
start
and
$ at the end...2d DOES match ^[+]?[0-9]\d* but it DOES NOT match
^[+]?[0-9]\d*$ (note the extra $ at the end). That's the difference
between the two.

Also, [0-9]\d* can be rewritten as \d+

Karl

If I have a regexvalidator in a webform with a validation expression of
^[+]?[0-9]\d* and I put the text 2d in a text box being validated by this
validator, the validator fires.

This regex is a simple one that only allows the user to enter integer
values.

However, if I do this on the server side like this

Regex rx = new Regex(@"^[+]?[0-9]\d*");
Match mt = rx.Match("2d");

I get success!!!! why?? is there an option I should be setting?
 
S

S. Justin Gengo

George,

You're welcome. Thanks for the praise. :)

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
George Durzi said:
That is a great tip!!! Thank you much! The regularexpressionvalidators do
seem like a little overkill for simple type checks

S. Justin Gengo said:
George,

Glad you got it working. (Good eyes Karl!)

Just a note: You could also use a CompareValidator instead of the
RegularExpressionValidator. If you leave the CompareValidator's
ControlToCompare property blank and set it's Operator property to
DataTypeCheck you may then set it's Type property to check if a value
entered is a String, Integer, Double, Date, or Currency.

While I love that I can do almost anything with the regular expression
validator I find simple data type checks much easier to set up with the
CompareValidator.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
George Durzi said:
Karl,
Thank you again, that worked perfectly.

And thanks for the regex refining tip :)

"Karl" <none> wrote in message
I think the regularexpressionvalidator automatically puts ^ at the start
and
$ at the end...2d DOES match ^[+]?[0-9]\d* but it DOES NOT match
^[+]?[0-9]\d*$ (note the extra $ at the end). That's the difference
between the two.

Also, [0-9]\d* can be rewritten as \d+

Karl

If I have a regexvalidator in a webform with a validation expression of
^[+]?[0-9]\d* and I put the text 2d in a text box being validated by
this
validator, the validator fires.

This regex is a simple one that only allows the user to enter
integer
values.

However, if I do this on the server side like this

Regex rx = new Regex(@"^[+]?[0-9]\d*");
Match mt = rx.Match("2d");

I get success!!!! why?? is there an option I should be setting?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top