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

人盡茶涼 提交于 2019-12-03 20:22:25

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

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.

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