开发者

asp.net regular expression not working as expected

开发者 https://www.devze.com 2022-12-23 15:49 出处:网络
I have a textbox and a regular expression validator applied to it. I want to make sure that the only allowed string i开发者_开发知识库nputted into the textbox are \"Anything Entered\" or \"Something E

I have a textbox and a regular expression validator applied to it. I want to make sure that the only allowed string i开发者_开发知识库nputted 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.


The RegularExpressionValidator automatically adds those ^ and $. Just use

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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消