Regular Expression Weirdness

  • Thread starter Thread starter George Durzi
  • Start date Start date
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?
 
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
 
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?
 
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?
 
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?
 
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?
 
Back
Top