MVC3 jquery 验证 MinLength 过滤器不起作用
我在我的模式下有这个:
[Required(AllowEmptyStrings = false, ErrorMessageResourceType = typeof(Registration), ErrorMessageResourceName = "NameRequired")]
[MinLength(3, ErrorMessageResourceType = typeof(Registration), ErrorMessageResourceName = "NameTooShort")]
public String Name { get; set; }
这最终会导致:
<div class="editor-label">
<label for="Name">Name</label>
</div>
<div class="editor-field">
<input class="text-box single-line" data-val="true" data-val-required="Name is required" id="Name" name="Name" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="Name" data-valmsg-replace="true"></span>
</div>
编译器怎么会忽略 MinLength?我怎样才能“打开它”?
I have this in my mode:
[Required(AllowEmptyStrings = false, ErrorMessageResourceType = typeof(Registration), ErrorMessageResourceName = "NameRequired")]
[MinLength(3, ErrorMessageResourceType = typeof(Registration), ErrorMessageResourceName = "NameTooShort")]
public String Name { get; set; }
This ends up in:
<div class="editor-label">
<label for="Name">Name</label>
</div>
<div class="editor-field">
<input class="text-box single-line" data-val="true" data-val-required="Name is required" id="Name" name="Name" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="Name" data-valmsg-replace="true"></span>
</div>
How come MinLength is being ignored by the compiler? How can I "turn it on"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
而不是使用
MinLength
属性使用此代替:字符串长度 MSDN
优点: 无需编写自定义属性
Instead of using
MinLength
attribute use this instead:String Length MSDN
Advantage: no need to write custom attribute
与其经历创建自定义属性的麻烦...为什么不使用正则表达式呢?
Instead of going through the hassle of creating custom attributes...why not use a regular expression?
最新版本的 ASP.Net MVC 现在支持 MinLength 和 MaxLength 属性。请参阅官方 asp.net mvc 页面: MinLengthAttribute 和 MaxLengthAttribute 的Unobtrusive 验证
The latest version of ASP.Net MVC now supports MinLength and MaxLength attributes. See the official asp.net mvc page: Unobtrusive validation for MinLengthAttribute and MaxLengthAttribute
检查这个问题。
阅读评论似乎 minlength 和 maxlenght 都不起作用。
因此他们建议使用 StringLength 属性作为 maxlenght。我想你应该为自定义属性的最小长度编写一个自定义属性
,你可以这样做
希望它有帮助
Check this question.
Reading the comments it seems that both minlength and maxlenght do not work.
So they suggest to use StringLength attribute for maxlenght. I guess you should write a custom attribute for the min legth
for the custom attribute you can do something like this
Hope it helps