views:

42

answers:

3

Hi,

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.

Thanks,

Zaps

+2  A: 
"^(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.

N 1.1
Not quite. That says "starts with 'Anything Entered' OR contains 'Something Else' OR ends with 'Another String'. You would need to put all three alternatives in one set of parens with the anchors outside them -- `^(foo|bar|baz)$` -- but as @Jens pointed out, the anchors are redundant in a Validator.
Alan Moore
@Alan: yes. but i just wanted to show the OP, the way `^` `$` would probably be used.
N 1.1
+2  A: 

The RegularExpressionValidator automatically adds those ^ and $. Just use

"(Anything Entered|something Else|Another String)"
Jens
A: 

(^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 ().

tiftik
What's wrong with the answer?
tiftik