0

Can I restrict user to enter only numbers using DataAnnotations?

Below is the property which I have in my Model

[Display(Name = "Fiscal Year")]
[Required(ErrorMessage = "Fiscal Year is required")]
public int FiscalYear { get; set; }

Below is the definition which I am using in .chtml (razor view):

@Html.TextBoxFor(model => model.project.FiscalYear)

I want to allow users only to enter numbers. Any suggestions?

Thanks, Balaji

4

2 回答 2

3

获取名为 DataAnnotationsExtensions.. 的 Nuget 包,然后像下面这样使用它:

    [Integer(ErrorMessage="This is needs to be integer")]
    public int CustomerId { get; set; }

假设您启用了 jQuery 验证插件和不显眼的验证,它将正确地进行验证。

于 2012-11-15T02:58:21.063 回答
0

您可以使用正则表达式属性

于 2012-11-14T19:29:59.440 回答