IsmailS
IsmailS

Reputation: 10863

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?

Upvotes: 2

Views: 11472

Answers (3)

Vinod
Vinod

Reputation: 4882

Try this:

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

Upvotes: 13

Shyju
Shyju

Reputation: 218732

The Max Value an Integer(Int32) can hold is 2,147,483,647. So you should better replace Int with Long or String.

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1038820

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.

Upvotes: 7

Related Questions