如何范围验证整数ASP.NET MVC 3

Ism*_*ilS 2 validation data-annotations asp.net-mvc-3

我正在使用如下验证.

[Required(ErrorMessage = "Please provide 10 digit mobile number without spaces and without country code (+91)")]
[Integer(ErrorMessage = "Please provide 10 digit mobile number without spaces and without country code (+91)")]
[Range(1000000000, 9999999999, ErrorMessage = "10 digit mobile number only without spaces and without country code (+91)")]
[Display(Name = "Mobile Number")]
public int MobileNo { get; set; }
Run Code Online (Sandbox Code Playgroud)

验证总是失败The value '9999999998' is invalid..难道我做错了什么?

Vin*_*nod 13

试试这个:

[RegularExpression("^[0-9]{10}$", ErrorMessage = "Invalid Mobile No")]
Run Code Online (Sandbox Code Playgroud)

  • 所有答案都有助于一种或另一种方式.非常感谢 :) (2认同)

Dar*_*rov 6

,一个最大值的Int32类型可以存储为2,147,483,648.你满溢了.为什么使用整数类型来表示电话号码?字符串似乎更适应.