user949914
user949914

Reputation: 221

Phone Number Length Validation in MVC3

[Integer]
[Required(ErrorMessage = "Please enter your phone number")]
public int Phone { get; set; }

I want to add Phone Number Length Validation, How can i add this.

If I use [MaxLength(10)] Than it shows Error!!!!

Upvotes: 0

Views: 3114

Answers (3)

armnov
armnov

Reputation: 659

You should consider to use phone number as string and use MaxLength to validate.

[Required(ErrorMessage="Please enter your phone number")]
[MaxLength(10, ErrorMessage="Cannot be longer than 10 characters")]
public string Phone { get; set; }

Upvotes: 0

Jakub Konecki
Jakub Konecki

Reputation: 46008

You need to store phone number as string, so I can type ie,. +447912345678 or 00447912345678.

Than you can use StringLength attribute.

Remember that users might type the number with spaces or hyphons, ie. 079 12 34 56 78, or 079-12-34-56-78.

Upvotes: 2

Iridio
Iridio

Reputation: 9271

In my opinion you should use a string for the phone number field. Obviosuly if it is not a mandatory business request.

Then you can apply the StringLengthAttribute

Upvotes: 0

Related Questions