如何使用mvc5中的数据注释验证移动号码

时间:2015-02-18 07:38:55

标签: asp.net-mvc data-annotations

我需要使用数据注释验证所有印度手机号码。在我的模型中,我使用以下代码

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

    public string ContactNumber { get; set; }

但我混淆了我需要写的代替[?]?请建议我验证手机号码的最佳方式。

2 个答案:

答案 0 :(得分:5)

试试这个希望它能帮到你。

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

答案 1 :(得分:4)

您可以添加此正则表达式代替[?]

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