Search code examples
c#asp.netregexaspx-user-control

Regular expression


I have an aspx:memo control in asp.net page. The control has a regular expression validator. If the text inserted in the memo is different than the regular expression then the validator triggers an error. So I would like to accept every character except these:

1) -- (double hyphen)
2) // (double slash)
3) ' (Single quote)
4) \\ (double backslash)
5) ^ (Caret)
6) ; (Semicolon)

So far I have created this expression:

^[\na-zA-Z0-9 .,~?`~!@():#&%=+΄<>\\\-\/_&quot;\]\[\}\{]*$

I have put inside every character that I accept in the memo. So the issue is that with this expression I accept slashes and backslashes or hyphens even if they are single or more. How can I disallow the double slashes or hyphens but allow single ones.

I have already lost a lot of time for this. Your help would be greatly appreciated.

Thank you in advance.


Solution

  • ^((?!--|\/\/|'|\\\\|\^|;).|\w)*$

    Breakdown

    (?! ) being the negative look ahead meaning that anything within (separated by |) will cause the validation to fail

    \/\/ being // with escaped characters

    \\\\ being \\ with escaped characters

    . allows any character (except whitespace)

    \w allows whitespace