带或不带国家代码的手机号码正则表达式 [英] Regex for Mobile Number with or without Country Code

查看:253
本文介绍了带或不带国家代码的手机号码正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在注册表单中验证我想要一个正则表达式的文本字段。

I need to validate a text field in my registration form for which I want a Regex.

文本框将接受手机号码,如9999999999(10位数 - 从7或8或9开始)。
用户也可以写+919999999999,其中+91是国家代码(印度为+91,但可以接受任何其他类似+110或+52),或者他也可以写09999999999(自己国家的前0)

The textbox will accept a mobile number like 9999999999(10 digit - starting with 7 or 8 or 9). The user may alternatively write +919999999999, where +91 is country code (+91 for India, but can accept any other like +110 or +52), or he may also write 09999999999 (first 0 for own country)

在这里,用户有3个选择,

Here, the user have 3 choices,


  1. 添加手机号码国家/地区代码

  2. 只是没有国家/地区代码或0前缀

  3. 在手机号码中以零为前缀。

虽然不是必需的,但我的页面是在asp.net中,我使用的是内置的正则表达式验证器。

Though not required, my page is in asp.net, and I am using built-in regular expression validator.

推荐答案

从我所看到的,这应该有效。前缀是可选的,存储在第一个匹配组中,主号码进入第二个组。

From what I can see, this should work. The prefix is optional and is stored into the first match group, with the main number going into the second group.

^([0|\+[0-9]{1,5})?([7-9][0-9]{9})$

但是如果你能为我们提供一些测试用例,它可以帮助我们为你提供你正常工作的正则表达式。

But if you can give us some test cases for each, it'd help us in giving you a working regex for what you want.

在对国家代码长度的正确限制的评论中向SchlaWiener道具。

Props to SchlaWiener in the comments for the correct limit on the country code length.

这篇关于带或不带国家代码的手机号码正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆