dav*_*990 28 asp.net-mvc minimum string-length data-annotations asp.net-mvc-4
我在MVC4中使用数据注释进行模型验证,并且我当前正在使用StringLengthAttribute但是我不想指定最大值(当前设置为50),但是DO想要指定最小字符串长度值.
有没有办法只指定最小长度?也许我可以使用另一个属性?
我目前的代码是:
[Required]
[DataType(DataType.Password)]
[Display(Name = "Confirm New Password")]
[StringLength(50, MinimumLength = 7)]
[CompareAttribute("NewPassword", ErrorMessage = "The New Password and Confirm New Password fields did not match.")]
public string ConfirmNewPassword { get; set; }
Run Code Online (Sandbox Code Playgroud)
任何帮助深表感谢.
Len*_*rri 44
有没有办法只指定最小长度?也许我可以使用另一个属性?
使用标准数据注释,否.您必须指定MaximumLength.只有其他参数是可选的.
在这种情况下,我建议这样的事情:
[StringLength(int.MaxValue, MinimumLength = 7)]
Run Code Online (Sandbox Code Playgroud)
您还可以使用像这样的正则表达式(正则表达式)属性:
[RegularExpression(@"^(?:.*[a-z]){7,}$", ErrorMessage = "String length must be greater than or equal 7 characters.")]
Run Code Online (Sandbox Code Playgroud)
更多相关信息:使用正则表达式验证密码强度
小智 8
为此,也有[MinLength(7)]属性。
来源:https : //msdn.microsoft.com/zh-cn/library/system.componentmodel.dataannotations.minlengthattribute(v=vs.110).aspx
归档时间: |
|
查看次数: |
42305 次 |
最近记录: |