Regular expression excluding these characters

I use MVC data annotations, and my requirement is that the address field can contain any characters except (for example, English characters) < > . ! @ # % / ? *.

I searched many sites but did not get how to write this regex.

So far I have tried:

[Required(ErrorMessage = "Address Required.")]
[RegularExpression(@"^[<>.!@#%/]+$", ErrorMessage = "Address invalid.")]
public string Address { get; set; }
+5
source share
4 answers

You are currently authorizing a string consisting only of these letters.

Using

"^[^<>.!@#%/]+$"
+10
source

Make your regular expression out of any characters other than those listed in the carriage:

[^abc] 

will match all that is not a, b or c.

So, putting all this together, your regular expression will be

^[^<>!@#%/?*]+$

, " ", " , , "

+8

Try regex:

[^<>.!@#%/?*]
+1
source

This should do the job:

"[^ <.!> @ #% /]"

EDIT

. (period) is a reserved character in regular expressions, so you need to avoid it.

+1
source

All Articles