Riain McAtamney
Riain McAtamney

Reputation: 6432

asp.net regular expression not working as expected

I have a textbox and a regular expression validator applied to it. I want to make sure that the only allowed string inputted into the textbox are "Anything Entered" or "Something Else" or "Another String" otherwise I want an error to be displayed.

This is the regular expression I have so far:

ValidationExpression="(^Anything Entered)$|(^Something Else)$ |(^Another String)$"

However when I enter the supposed valid strings the error is displayed. I cant figure out whats wrong with the expression. Any help would be greatly appreciated.

Upvotes: 0

Views: 370

Answers (3)

N 1.1
N 1.1

Reputation: 12534

"^(Anything Entered)|(Something Else)|(Another String)$"

Note the use of ^ and $.
Although, as others have already pointed out, using ^ $ is redundant here.

"(Anything Entered|Something Else|Another String)" is just fine.

Upvotes: 2

tiftik
tiftik

Reputation: 988

(^Anything Entered)$|(^Something Else)$ |(^Another String)$

In regex ^ matches the beginning of the string and $ matches the end of the string.

Your regex is equivalent to (^Anything Entered$)|(^Something Else$ )|(^Another String$). It matches "Anything Entered" or "Another String" but it doesn't match "Something Else" because there can't be a space after the end of the string ().

Upvotes: -1

Jens
Jens

Reputation: 25563

The RegularExpressionValidator automatically adds those ^ and $. Just use

"(Anything Entered|something Else|Another String)"

Upvotes: 2

Related Questions