使用正则表达式验证手机号码 [英] Validate Mobile number using regular expression

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

问题描述

我需要验证手机号码.我的需求:

<块引用>

  1. 号码可能以 +8801 或 8801 或 01 开头
  2. 下一个数字可以是 1 或 5 或 6 或 7 或 8 或 9
  3. 然后有精确的 8 位数字.

如何使用此条件编写正则表达式?

我试过的手机号码

+8801811419556018114195568801711419556016114195568801511419556

解决方案

应该很简单:

^(?:\+?88)?01[15-9]\d{8}$

  • ^ - 从字符串开始
  • (?:\+?88)? - 可选的 88可以+<开头/li>
  • 01 - 强制01
  • [15-9] - 1 或 5 或 6 或 7 或 8 或 9"
  • \d{8} - 8 位数字
  • $ - 字符串结束

工作示例:http://rubular.com/r/BvnSXDOYF8

2020 年更新

由于 BTRC 批准了 2 个新前缀,即 Grameenphone 的 013 和 Banglalink 的 014,现在更新了表达式:

^(?:\+?88)?01[13-9]\d{8}$

I need to validate mobile number. My need:

  1. The number may start with +8801 or 8801 or 01
  2. The next number can be 1 or 5 or 6 or 7 or 8 or 9
  3. Then there have exact 8 digit.

How can i write the regular expression using this conditions ?

the mobile numbers I tried

+8801811419556
01811419556
8801711419556
01611419556
8801511419556

解决方案

Should be pretty simple:

^(?:\+?88)?01[15-9]\d{8}$

  • ^ - From start of the string
  • (?:\+?88)? - optional 88, which may begin in +
  • 01 - mandatory 01
  • [15-9] - "1 or 5 or 6 or 7 or 8 or 9"
  • \d{8} - 8 digits
  • $ - end of the string

Working example: http://rubular.com/r/BvnSXDOYF8

Update 2020

As BTRC approved 2 new prefixes, 013 for Grameenphone and 014 for Banglalink, updated expression for now:

^(?:\+?88)?01[13-9]\d{8}$

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

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