Namit Kumar
Namit Kumar

Reputation: 121

How to validate mobile Number using data annotation in mvc5

I need to validate all the Indian mobile number using data annotation. in my model i use following code

    [Required]
    [Display(Name = "Mobile Number")]
    [CustomRemote("IsMobileNoAvailable", "Employees",
     ErrorMessage = "Mobile No already in use", AdditionalFields = "Id")]
    [?]

    public string ContactNumber { get; set; }

but i am confuse what i need to write in place of [?] ? Please suggest me the best way to validate mobile number.

Upvotes: 0

Views: 8497

Answers (2)

user3966829
user3966829

Reputation:

Try this one hope it will help you.

[RegularExpression(@"^([0]|\+91[\-\s]?)?[789]\d{9}$", ErrorMessage = "Entered Mobile No is not valid.")]

Upvotes: 5

Tushar Gupta
Tushar Gupta

Reputation: 15923

You can add this regex in place of [?]

[RegularExpression(@"^((\+91-?)|0)?[0-9]{10}$", ErrorMessage = "Entered phone format is not valid.")]

Upvotes: 4

Related Questions