sukesh
sukesh

Reputation: 2423

MVC regex to not allow spaces and special characters

I have a Regex to not allow spaces and some special chars., but the validation fires even when Uppercase letters are entered.

[RegularExpression(@"^[^<>.,?;:'()!~%-_@#%/*""\s]+$")]
public string FirstName { get; set; }

Upvotes: 2

Views: 6776

Answers (1)

user3559349
user3559349

Reputation:

You need to remove the - (minus sign). %-_ means between % (char code 37) and _ (char code 95) which includes upper case characters.

If you want to exclude the minus symbol, then you need to escape it using \-.

The attribute should be

[RegularExpression(@"^[^<>.,?;:'()!~%\-_@#/*""\s]+$")]
public string FirstName { get; set; }

Upvotes: 6

Related Questions