I am trying to achieve the following using regex in C# in .Net application
I need to restrict strings which contains 'ONLY' these three special characters namely #,@ and %. Any other character is allowed
some examples
ab#c - allowed
abc#@ - allowed
#%abc - allowed
#@% - not allowed
##@ - not allowed
What I tried
^(?!(#|@|%)+$)
But it doesn't seem to work as expected. Where am I going wrong ?
I am using data annotations on the property, so I am looking for a regex pattern that can exclude the incorrect input strings. I cannot use the Regex.IsMatch()
function.
Clarification: My string should accept any characters. The only case it should fail is when the input string only has #,@ or %.