【问题标题】:Do not allow special characters in NumericUpDown EditorFor() in ASP.NET MVCASP.NET MVC 中的 NumericUpDown EditorFor() 中不允许使用特殊字符
【发布时间】:2013-10-16 11:17:44
【问题描述】:

我想根据我的属性 (int) 显示一个 NumericUpDown 控件。它有效,但它允许我输入特殊字符,如 #$%&..

我的 .cshtml 代码是:

@Html.EditorFor(m => m.SerialAddress, "MyControlName")

我的模型中的属性是:

[Required]
[Display(ResourceType = typeof (WebResources), Name = WebResources.LabelAddress)]
public int SerialAddress { get; set; }

我该怎么做?我的属性是一个 int 但控件允许我写 #$%&

【问题讨论】:

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


    【解决方案1】:

    试试这个

    [Required]
    [Display(ResourceType = typeof (WebResources), Name = WebResources.LabelAddress)]
    [Required(ErrorMessage = "Please enter Numeric Only")]
    [RegularExpression(@"^\d+$", ErrorMessage = “Enter Numeric only”)]
    
    public int SerialAddress { get; set; }
    

    【讨论】:

      【解决方案2】:

      设置模型属性为

      如果你只想要不,因为你的属性是 int。

      [Required]
      [Display(ResourceType = typeof (WebResources), Name = WebResources.LabelAddress)]
      [RegularExpression(@"^[1-9]\d+$", ErrorMessage = "invalid character")]
      public int SerialAddress { get; set; }
      

      【讨论】:

        猜你喜欢
        • 2021-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多