开发者

Regular expression multiline validator

In my ASP.NET Web Form I have a multiline TextBox which should be validated with RegularExpression Validator. Text box should contain one or more strings "a" (just 'a' char, nothing else).

So far I got these regular expressions for my RegularExpressionValidator object:

(?m:(^a$)+)
(?m:\A(^a$)+\Z)
(?m:^a$)

and some others. Neither works. Guess there is something fundamental I'm not getting yet.

Could you please tell me where I'm wrong?

Here's the code involved.

A Button (just for postbacks):

<asp:Button ID="Button1" runat="server" Text="Button" />

The TextBox:

<asp:TextBox ID="TextBox1" runat="server" Rows="10" TextMode="MultiLine"></asp:TextBox>

And the regex validator:

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
    ControlToValidate="TextBox1" ErrorMessage="RegularExpressionValidator" 
    ValidationExpression="(?m:(^a$)+)"></asp:RegularExpr开发者_StackOverflow社区essionValidator>

There is nothing else on that Web Form. I've only added those controls and modified properties. I've even did all this using VS GUI.


Using CustomValidator and doing Regex.Match(TextBox1, @"(?m:(^a$)+)") in it works just fine. Something is wrong with RegularExpressionValidator I guess.


If you want to match multiple lines, don't forget to also match the line terminators; they are not implied in $.

(?m:(^a$\r?\n?)+)

might work better.

This matches

a

or

a
a
a

etc.

And, since you're asking for a tutorial, how about regular-expressions.info?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜