我想阻止用户名中的空格和特殊字符,但我不知道如何在一个数据属性中执行此操作,而asp.net mvc只允许每个属性使用1个正则表达式属性。
[Required()]
[StringLength(10, MinimumLength = 6, ErrorMessage = "Min 6 and max 10 characters")]
[Display(Name = "User Name")]
[RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed")]
[RegularExpression(@"^[\%\/\\\&\?\,\'\;\:\!\-]+$", ErrorMessage = "No special characters")]
[Remote("IsUsernameDuplicate", "Account", HttpMethod = "Get", ErrorMessage = "The username is already in use")]
public string UserName { get; set; }
编辑1 我已将属性更改为
[Required()]
[MinLength(5, ErrorMessage = "Minimum length is 5")]
[MaxLength(12, ErrorMessage = "Maximum length is 12")]
[Display(Name = "User Name")]
[RegularExpression(@"^[A-Za-z0-9_]$", ErrorMessage = "Hey, no funny symbol stuff")]
[Remote("IsUsernameDuplicate", "Account", HttpMethod = "Get", ErrorMessage = "The username is already in use")]
public string UserName { get; set; }
但是我在输入chuck并按Tab键时出现错误
编辑2
用^ [A-Za-z0-9 _] + $
的reg ex解决了它答案 0 :(得分:0)
使用A-Za-z0-9
的用户名的正则表达式,只允许_
(与大多数用户名一样)。
正则表达式: ^[A-Za-z0-9_]{5,12}$
由于您的最小和最大长度已经过检查,因此您可以使用^[A-Za-z0-9_]+$
。