views:

221

answers:

1

Hi!

I believe it is a simple question. :)

In my ASP.NET Web Form I have a multiline TextBox which should be validated with RegularExpression Validator. Text box should contain one ore 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? And what to read so I won't ask such stupid questions again? :)

Thanks!

UPD:

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:RegularExpressionValidator>

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.

UPD2:

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

A: 

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?

Tim Pietzcker
Thanks Tim, but it doesn't work. (?m:(^a\r?\n?$)+) this one doesn't work either. But (?m:(^a\r?\n?)+) does. There is something else about $ :)
kishkin
Thanks for the link! Done that. But I'll go look through it again, in case I missed the answer for my question.
kishkin
The last regex you wrote in your comment is exactly the same one in my answer. What do you mean?
Tim Pietzcker
No, it's not. There is no $ sign.
kishkin
Oh, right. But that shouldn't matter. I've just tested my regex, and it works as advertised. Perhaps you should update your question with the actual code you're using - the problem might be elsewhere.
Tim Pietzcker
Strange. It does match on regexlib.com, but not in my app. There is nothing but this textbox and validator object. And I'm just putting this regex into ValidationExpression field.
kishkin
And you're sure you don't have any additional whitespace anywhere?
Tim Pietzcker
Yes, I'm sure. Just in case I've just checked.
kishkin
OK, the Regex is obviously correct, checked with RegexBuddy, Python, and regexlib. So the error is elsewhere. I don't know the validator object and can't help you further with that.
Tim Pietzcker
Thank you Tim! I really appreciate your help.
kishkin
Found the problem: Unescaped backslashes.
Tim Pietzcker
I'm afraid it didn't worked. And again (?m:(^a\r?\n?)+) does work, so I don't think that those backslashes are unescaped.
kishkin
Hm. Well then I give up. At least I tried :)
Tim Pietzcker
Ok, thanks a lot!
kishkin
Doing regex match using Regex.Match(TextBox1, @"(?m:(^a$\r?\n?)+)") in CustomValidator object everything works just fine. Something is wrong with RegularExpressionValidator I guess.
kishkin