How can I validate Mobile/Landline Number formatting using MVC.Net?

╄→гoц情女王★ 提交于 2019-12-05 02:15:12

问题


Pretty simple question - validating International Mobile/Landline Number using Data Annotations.

Any suggestions?


回答1:


There is no single "international" phone number standard, but you can do some validation. For example:

[RegularExpression(@"^([\+]|0)[(\s]{0,1}[2-9][0-9]{0,2}[\s-)]{0,2}[0-9][0-9][0-9\s-]*[0-9]$")]

This allows, for example: 044 123-456, +2 12-12456, +(234) 56-56-452. See here for more kinds of regular expressions for phone numbers:

http://regexlib.com/Search.aspx?k=phone+number&c=0&m=0&ps=20&p=12




回答2:


MVC has built in support for telephone numbers:

[DataType(DataType.PhoneNumber)]
public string Phone { get; set; }

See the MSDN reference page for more information on the validatation types available: DataType Enumeration.



来源:https://stackoverflow.com/questions/8028735/how-can-i-validate-mobile-landline-number-formatting-using-mvc-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!