0

This is very basic and not thorough validation I know but the textbox needs to accept for example name@email.com as well as 4556-222-44444.

I've tried

<asp:RegularExpressionValidator Display="Dynamic" runat="server" ControlToValidate="txtAddToBlacklist" 
ErrorMessage="Invalid" ValidationExpression="^[\-\@]$"></asp:RegularExpressionValidator>

This attempt included the escaping backslashes to no avail.

My logic for using "[-@]" to check for either to be present came from this answer.

I know that the hyphen has to be at the beginning or the end but as it's only two characters, I don't think this is the issue.

The ^ and $ are included as that seems to be recommended practice to prevent malicious extras being appended.

Must be missing something though so any help is appreciated!

Community
  • 1
  • 1
embarr
  • 3
  • 1
  • 4
  • What if you don't put ^ and $? As it is, your regular expression does not allow any other character than one that belongs to the character class. – ConnorsFan Jun 06 '16 at 13:51
  • @ConnorsFan Doesn't seem to make any difference without them unfortunately. – embarr Jun 06 '16 at 14:02
  • It may be the character escaping (I haven't checked that). You can test your regular expression on various Web sites, like this one: https://regex101.com/. It will allow you to experiment. (It seems to me that `[-@]` works). – ConnorsFan Jun 06 '16 at 14:03
  • Shouldn't it be `^.*[-@].*$`. The `^` and `$` are the start and end of the string, so you want characters in between. Otherwise you are just checking against the string "@" and "-". – H. de Jonge Jun 06 '16 at 14:16
  • @H.deJonge Your proposed answer "^.*[-@].*$" works, do you want to repost as an answer? As an extra though, would you know how to make this work for a multiline textarea? (as in include new lines) – embarr Jun 06 '16 at 14:34
  • @H.deJonge Ignore the q about multiline, I got it working by using your answer with the [\s\S] to include new lines. Using this now: "^[\s\S]*[-@][\s\S]*$" – embarr Jun 06 '16 at 14:42
  • If you accept anything before and after one of the two characters, is your expression any different from just `[-@]`? – ConnorsFan Jun 06 '16 at 14:57
  • @ConnorsFan The main difference I'm aware of is that [-@] didn't work, yet [\s\S]*[-@][\s\S]* did. As other than that they seem to do the same thing. – embarr Jun 06 '16 at 15:25
  • You are right. The regex tester tells me that `[-@]` matches the same strings but the ASP.NET validator does not. It must be looking for some kind of "full match". – ConnorsFan Jun 06 '16 at 16:13

1 Answers1

0

The ^ and $ are resp. the start and end of the string. This means that your regex just tests against the string "@" and "-".

If you use ^.+[-@].+$, you will test against strings containing the mentioned characters inside the string.

For multiline you might want to try something like this ^(.+[@-].+\n?)*$. (There's still some puzzling needed for this one. I added the ? after the newline because it should not be mandatory on the last line, but this way also "bla@bla.com@test.lan" will test positive.) Tweak it to fit your needs.

I'm not a regex-king, but use an online regex tool, such as http://www.regexr.com/, to build and test my expressions.

As a side-note: To gain regex skills you can try to solve these https://regexcrossword.com/

H. de Jonge
  • 878
  • 7
  • 25