html.textboxfor() 应该正好是 7 个字符
我需要一个恰好包含 7 个字符的文本框, 为此我写了
Html.TextboxFor(x=>x.Number, new {maxLength = "7"};
这个案例只需要7个字符,但是如果我想少于7个字符,那么需要什么? 是否有像 maxlength
这样的属性只需要 7 个字符。
问候, 迈克尔·韦拉亚杜
I need a text box which should be exactly of 7 characters,
for which I have written
Html.TextboxFor(x=>x.Number, new {maxLength = "7"};
This case takes me only 7 characters , but if I want to take less than 7, it is taking?
Is there any property like maxlength
which takes 7 charecters only.
regards,
michael velayadu
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于其价值,这些答案都不是很清楚。您想要做的本质上是设置最小和最大长度属性。基本上有几种方法可以使用数据注释来完成此任务。
最简单的方法:
使用 StringLengthAttribute... StringLength 有两个重载方法。两者都有 int 参数
@maximumLength
,并且重载之一包括 NamedParameters。因此,对于 maxlength,您不会使用命名参数,但对于 minlength,您会:如果您想真正感到困惑,您也可以这样做:
For what its worth, none of these answers are very clear. What you are trying to do is set a minimum and maximum length attribute essentially. There are basically a few ways you can accomplish this using data annotations.
The easiest way:
Using StringLengthAttribute... StringLength has two overloaded methods. Both have the int parameter
@maximumLength
and one of the overloads includes NamedParameters. So for maxlength you wouldn't use a named parameter but for minlength you would:If you want to be really confusing you could also do it this way:
通过使用正则表达式的 DataAnnotations,您可以强制执行恰好 7 个字符的字符串。 DataAnnotations 也可以在客户端使用,您只需启用它即可。
查看此帖子以获取更多信息: http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx
With DataAnnotations using regular expressions you can enforce a string of exactly 7 charachters. DataAnnotations can also be used client-side, you just have to enable it.
Check this post for more info: http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx
您不需要为此创建自定义验证器。您可以使用 StringLength 属性来实现您想要的开箱即用:
You do not need to create a custom validator for this. You can use the StringLength attribute to achieve what you want out of the box:
文本框上没有最小长度的 Html 属性。您可以使用数据注释来强制该字段的最大和最小文本长度...
这是使用数据注释的一个很好的演练:Stephen Walther 在 ASP.Net 上
There is no Html attribute on a textbox for Minimum Length. You can use data annotations to enforce a maximum and minimum text length for that field...
This is a pretty good walkthrough on using data annotations: Stephen Walther on ASP.Net