0

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.

1
  • Are you sure it is correct to put "^" and "$" symbols on the different sides of the brackets? Commented Mar 16, 2010 at 11:52

3 Answers 3

2

The RegularExpressionValidator automatically adds those ^ and $. Just use

"(Anything Entered|something Else|Another String)"
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

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.
-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.