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

标签 asp.net-mvc asp.net-mvc-4 data-annotations minimum string-length

我正在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)]

您也可以使用Regex(正则表达式)属性,如下所示:
[RegularExpression(@"^(?:.*[a-z]){7,}$", ErrorMessage = "String length must be greater than or equal 7 characters.")]

此处的更多信息:Password Strength Validation with Regular Expressions

关于asp.net-mvc - DataAnnotations StringLength属性MVC-没有最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11404486/

相关文章:

asp.net-mvc - 如何使用 Html.ActionLink 通过 MVC3 指定准确的 URL

asp.net-mvc - ASP.NET MVC : redirect to action using URI

javascript - 远程片段上的 Bootstrap 模态 'loaded' 事件

c# - 编写 CompareTo DataAnnotation 属性

c# - Orchard Shape 类型 ResizeMediaUrl 未找到异常,MediaLibrary 组件不工作

javascript - MVC4 ajax调用并正确返回数据

c# - Microsoft ASP.NET Web Pages 2 Data Nuget 包有什么用?

c# - iTextSharp 输入字符串格式不正确 css 错误

asp.net-mvc - MVC 3 : Hide ID property using EditorForModel

c# - 在 C# 中验证列表