2

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?

3 Answers 3

13

Try this:

[RegularExpression("^[0-9]{10}$", ErrorMessage = "Invalid Mobile No")]
Sign up to request clarification or add additional context in comments.

1 Comment

All answers were helpful in one or other way. Thanks very much :)
7

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.

4 Comments

I thought specifying range would be easy on numeric that's why. No special reason. I mean I thought if I take string then it will have to do two checks i.e. length and numerics.
You could use a regular expression.
All answers were helpful in one or other way. Thanks very much :)
I'm simply not able to understand RegEx yet. :(. @Vinod's answer will help for now.
2

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

1 Comment

All answers were helpful in one or other way. Thanks very much :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.