加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – DataAnnotations StringLength属性MVC – 没有最

发布时间:2020-12-15 18:32:05 所属栏目:asp.Net 来源:网络整理
导读:我在MVC4中使用数据注释进行模型验证,目前我正在使用StringLengthAttribute,但是我不想指定最大值(当前设置为50),但是要指定最小字符串长度值。 有没有办法只指定最小长度?也许我可以使用另一个属性? 我当前的代码是: [Required] [DataType(DataType.P
我在MVC4中使用数据注释进行模型验证,目前我正在使用StringLengthAttribute,但是我不想指定最大值(当前设置为50),但是要指定最小字符串长度值。

有没有办法只指定最小长度?也许我可以使用另一个属性?

我当前的代码是:

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

任何帮助深表感谢。

解决方法

Is there a way to only specify only a minimum length? Perhaps another
attribute i can use?

使用标准数据注释,否。您必须指定MaximumLength。只有其他参数是可选的。

在这种情况下,我建议这样做:

[StringLength(int.MaxValue,MinimumLength = 7)]

您还可以使用像这样的正则表达式(正则表达式)属性:

[RegularExpression(@"^(?:.*[a-z]){7,}$",ErrorMessage = "String length must be greater than or equal 7 characters.")]

更多关于这里:Password Strength Validation with Regular Expressions

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读