Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Range validation integer ASP.NET MVC 3

I'm using validations as below.

[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; }

It is always failing the validations saying The value '9999999998' is invalid.. Am I doing something wrong?

like image 421
IsmailS Avatar asked May 27 '26 14:05

IsmailS


2 Answers

Try this:

[RegularExpression("^[0-9]{10}$", ErrorMessage = "Invalid Mobile No")]
like image 111
Vinod Avatar answered May 30 '26 03:05

Vinod


The maximum value that an Int32 type could store is 2,147,483,648. You are overflowing. Why are you using an integer type to represent a phone number? String seems more adapted.

like image 43
Darin Dimitrov Avatar answered May 30 '26 03:05

Darin Dimitrov