C# DataAnnotations[Phone]属性

C# DataAnnotations[Phone]属性,c#,regex,asp.net-mvc,validation,data-annotations,C#,Regex,Asp.net Mvc,Validation,Data Annotations,[Phone]属性的默认有效格式是什么? 在数据表中,phone列为navrchar(16) 如果我输入一个电话,比如111222333,我会得到“字段不是有效的电话号码。” 如果输入011122333,则得到“值'11112223333'无效” 还有,如何覆盖它? 我知道我可以这样做,但这是这种情况下的最佳做法吗 [RegularExpression(@"((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}",ErrorMessage="Invalid Phone Numb

[Phone]属性的默认有效格式是什么? 在数据表中,phone列为navrchar(16) 如果我输入一个电话,比如111222333,我会得到“字段不是有效的电话号码。” 如果输入011122333,则得到“值'11112223333'无效”

还有,如何覆盖它? 我知道我可以这样做,但这是这种情况下的最佳做法吗

[RegularExpression(@"((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}",ErrorMessage="Invalid Phone Number!")]
相关代码:

    [Required]
    [Phone]
    public string Phone { get; set; }

    <div class="editor-field">
       @Html.EditorFor(model => model.Phone)
       @Html.ValidationMessageFor(model => model.Phone)
    </div>
[必需]
[电话]
公用字符串电话{get;set;}
@EditorFor(model=>model.Phone)
@Html.ValidationMessageFor(model=>model.Phone)
更新 我想当我将phone列从int改为navrchar时,有一个映射问题。更新模型是不够的,所以我必须使用表映射手动更改值

错误:指定的成员映射无效。 成员“Phone”的类型“Edm.Int32[Nullable=False,DefaultValue=]” “UserDBModel.UserProfile”类型中的“”与“”不兼容 'SqlServerCe.nvarchar[Nullable=False,DefaultValue=,MaxLength=16,Unicode=True,FixedLength=False]' “UserDBModel.Store.UserProfile”类型中的成员“Phone”的名称


现在可以通过使用或浏览源代码轻松找到
phone属性的默认正则表达式

在这里,它将(丑陋的)正则表达式定义为:

private static Regex\u Regex=new Regex(@“^(+\s?)((?试试这个-

  [Required(ErrorMessage = "Mobile no. is required")]
  [RegularExpression("^(?!0+$)(\\+\\d{1,3}[- ]?)?(?!0+$)\\d{10,15}$", ErrorMessage = "Please enter valid phone no.")]
  public string Phone { get; set; }

这是一个很好的实践。如果需要多次使用电话验证,最好创建自己的验证属性(PhoneNumberAttribute)。您将数据库中的列更改为“nvarchar”,但将
Phone
属性保留在
UserDBModel.UserProfile
an
int
中,该正则表达式的样本号是多少?我无法从正则表达式中重新创建它。请小心使用正则表达式。并非所有浏览器都支持表达式中的负数,尤其是夏利