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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11404486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:05:54  来源:igfitidea点击:

DataAnnotations StringLength Attribute MVC - without max value

asp.net-mvcasp.net-mvc-4data-annotationsminimumstring-length

提问by davey1990

I am using Data Annotations for Model validation in MVC4 and am currently using the StringLengthAttribute however i do NOT want to specify a maximum value (currently set at 50) but DO want to specify a minimum string length value.

我在 MVC4 中使用数据注释进行模型验证,目前正在使用 StringLengthAttribute,但是我不想指定最大值(当前设置为 50),但想指定最小字符串长度值。

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

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

My current code is:

我目前的代码是:

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

回答by Leniel Maccaferri

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

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

Using the standard data annotation, No. You must specify the MaximumLength. Only the other parameters are optional.

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

In such a case, I'd recommend something like this:

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

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

You can also use a Regex (regular expression) attribute like this one:

您还可以使用 Regex(正则表达式)属性,如下所示:

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

More on this here: Password Strength Validation with Regular Expressions

更多相关信息:使用正则表达式进行密码强度验证

回答by Francois Muller

Have you thought of removing the data annotations and add an Html attribute to the Html.TextBoxFor element in your View?

你有没有想过删除数据注释并在你的视图中为 Html.TextBoxFor 元素添加一个 Html 属性?

Should look something like:

应该看起来像:

@Html.TextBoxFor(model => model.Full_Name, new { htmlAttributes = new { @class = "form-control", @minlength = "10" } })

or

或者

@Html.TextBoxFor(model => model.Full_Name, new { @class = "form-control",  @minlength = "10" } })

10 being the minimum length you choose.

10 是您选择的最小长度。

I like to add html attributes to my view as I can quickly see the impact it has. without messing with your database and does not require you to run migrations and database update if your using migrations (code first approach).

我喜欢将 html 属性添加到我的视图中,因为我可以快速看到它的影响。不会弄乱您的数据库,并且如果您使用迁移(代码优先方法),则不需要您运行迁移和数据库更新。

just remember that when you change the EditorFor to TextBoxFor you will loose your styling but should be an easy fix, again you can just add the styles to your view or add styling to your CSS file.

请记住,当您将 EditorFor 更改为 TextBoxFor 时,您将失去样式,但应该是一个简单的修复,您可以再次将样式添加到您的视图或将样式添加到您的 CSS 文件。

Hope this helps :)

希望这可以帮助 :)

回答by Madog

Nice work Francois, It seems you cannot use data annotations to emit a maxlength attribute on the input field. It just provides client-side validation. Using maxlength with HTML attributes created the input field with the maxlength set correctly.

好工作弗朗索瓦,看来您不能使用数据注释在输入字段上发出 maxlength 属性。它只提供客户端验证。将 maxlength 与 HTML 属性一起使用会创建正确设置 maxlength 的输入字段。

@Html.EditorFor(model => model.Audit_Document_Action, new { htmlAttributes = new { @class = "form-control", @maxlength = "10" } })