简体   繁体   中英

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; }

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; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM