C# DataAnnotations [电话] 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19595135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:23:13  来源:igfitidea点击:

The DataAnnotations [Phone] Attribute

c#regexasp.net-mvcvalidationdata-annotations

提问by usefulBee

What is the default, valid format of the [Phone] attribute? In the data table, the phone column is navrchar (16) If I enter a phone # like 1112223333, I get "field is not a valid phone number." If I enter 01112223333, I get "The value '11112223333' is invalid."

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

Also, how to override it? I understand that I could do something like this, but is this the best practice in this case?

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

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

Related code:

相关代码:

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

    <div class="editor-field">
       @Html.EditorFor(model => model.Phone)
       @Html.ValidationMessageFor(model => model.Phone)
    </div>

UpdateI guess there was a mapping issue when I changed the phone column from int to navrchar. Updating the model was not enough, so I had to change the value manually using the Table Mapping.

更新我猜当我将电话列从 int 更改为 navrchar 时存在映射问题。更新模型是不够的,所以我不得不使用表映射手动更改值。

Error 2019: Member Mapping specified is not valid. The type 'Edm.Int32[Nullable=False,DefaultValue=]' of member 'Phone' in type 'UserDBModel.UserProfile' is not compatible with 'SqlServerCe.nvarchar[Nullable=False,DefaultValue=,MaxLength=16,Unicode=True,FixedLength=False]' of member 'Phone' in type 'UserDBModel.Store.UserProfile'.

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

回答by Matt Tester

The default regular expression for the PhoneAttributecan now be handily found by browsing the source code with .NET Reference Source(.NET Framework 2.7.2)or source.dot.net(.NET Core)

PhoneAttribute现在可以通过使用.NET Reference Source( .NET Framework 2.7.2)source.dot.net( .NET Core)浏览源代码轻松找到的默认正则表达式

There it shows the (ugly) Regex as being defined as:

在那里它显示(丑陋的)正则表达式被定义为:

private static Regex _regex = new Regex(@"^(\+\s?)?((?<!\+.*)\(\+?\d+([\s\-\.]?\d+)?\)|\d+)([\s\-\.]?(\(\d+([\s\-\.]?\d+)?\)|\d+))*(\s?(x|ext\.?)\s?\d+)?$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);

That answers your direct question, but whether it helps or not remains to be seen. Maybe it would be a good base to create your own modified phone number regex.

这回答了你的直接问题,但它是否有帮助还有待观察。也许创建自己的修改后的电话号码正则表达式是一个很好的基础。

Sample Regex Matches

示例正则表达式匹配

回答by Sayli Vaidya

Try this -

尝试这个 -

  [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; }