【问题标题】:I would like to make an EditFor textbox accept numbers only我想让 EditorFor 文本框只接受数字
【发布时间】:2011-09-05 16:45:30
【问题描述】:

这是我的 ViewModel:

[Required(ErrorMessageResourceType=typeof(Resources.ValidationsResources),
          ErrorMessageResourceName = "Required")]
[Range(0, 9999, 
       ErrorMessageResourceType = typeof(Resources.ValidationsResources),
       ErrorMessageResourceName = "SomenteNumeros")]
public Int16 Quantidade { get; set;}

这是我的观点:

 <label class="Quantidade">
   <span>Quantidade</span>
   @Html.EditorFor(model => model.Quantidade)
 </label>

但我想屏蔽这个编辑器中的所有单词,只允许数字。

我该怎么做?

【问题讨论】:

    标签: c# asp.net-mvc validation razor


    【解决方案1】:

    为此,您需要 javascript。有很多jQuery plugins 可以帮助您实现这一点。

    【讨论】:

      【解决方案2】:

      使用正则表达式,例如

      [RegularExpression("([1-9][0-9]*)", ErrorMessage = "Count must be a natural number")]
      public int Count { get; set; }
      

      【讨论】:

        猜你喜欢
        • 2015-12-30
        • 2022-06-28
        • 2011-06-15
        • 2017-11-22
        • 1970-01-01
        • 2013-12-05
        • 1970-01-01
        • 1970-01-01
        • 2012-02-19
        相关资源
        最近更新 更多