MVC3 jquery 验证 MinLength 过滤器不起作用

发布于 2024-12-01 11:29:59 字数 818 浏览 0 评论 0原文

我在我的模式下有这个:

[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

高冷爸爸 2024-12-08 11:29:59

而不是使用 MinLength 属性使用此代替:

[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]

字符串长度 MSDN

优点: 无需编写自定义属性

Instead of using MinLength attribute use this instead:

[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]

String Length MSDN

Advantage: no need to write custom attribute

祁梦 2024-12-08 11:29:59

与其经历创建自定义属性的麻烦...为什么不使用正则表达式呢?

// Minimum of 3 characters, unlimited maximum
[RegularExpression(@"^.{3,}$", ErrorMessage = "Not long enough!")]

// No minimum, maximum of 42 characters
[RegularExpression(@"^.{,42}$", ErrorMessage = "Too long!")]

// Minimum of 13 characters, maximum of 37 characters
[RegularExpression(@"^.{13,37}$", ErrorMessage = "Needs to be 13 to 37 characters yo!")]

Instead of going through the hassle of creating custom attributes...why not use a regular expression?

// Minimum of 3 characters, unlimited maximum
[RegularExpression(@"^.{3,}$", ErrorMessage = "Not long enough!")]

// No minimum, maximum of 42 characters
[RegularExpression(@"^.{,42}$", ErrorMessage = "Too long!")]

// Minimum of 13 characters, maximum of 37 characters
[RegularExpression(@"^.{13,37}$", ErrorMessage = "Needs to be 13 to 37 characters yo!")]
暖阳 2024-12-08 11:29:59

最新版本的 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

甩你一脸翔 2024-12-08 11:29:59

检查这个问题
阅读评论似乎 minlength 和 maxlenght 都不起作用。
因此他们建议使用 StringLength 属性作为 maxlenght。我想你应该为自定义属性的最小长度编写一个自定义属性

,你可以这样做

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class MyMinLengthAttribute : ValidationAttribute
{
  public int MinValue { get; set; }  

  public override bool IsValid(object value)
  {
    return value != null && value is string && ((string)value).Length >= MinValue;
  }
}

希望它有帮助

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

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class MyMinLengthAttribute : ValidationAttribute
{
  public int MinValue { get; set; }  

  public override bool IsValid(object value)
  {
    return value != null && value is string && ((string)value).Length >= MinValue;
  }
}

Hope it helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文