基于 GB 且仅数字电话号码的正则表达式 [英] Regular expression for GB based and only numeric phone number

查看:27
本文介绍了基于 GB 且仅数字电话号码的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个仅允许数字电话号码(例如 1234567890)以及 GB 格式号码(例如 123-456-7890)的正则表达式代码.

I want a regular expression code which allows only numeric phone number (e.g 1234567890) as well as GB format number (e.g 123-456-7890).

此表达式必须同时适用于两种情况.

This expression must have to work for both conditions.

目前我正在使用下面的正则表达式,它只允许 GB 电话号码

Currently I am using below regular expression which only allows GB phone number

/^(()?d{3}())?(-|s)?d{3}(-|s)d{4}$/ 

我需要帮助才能只允许数字.

I need help to allow only numeric number also.

推荐答案

有一些英国电话号码正则表达式 此处.

There are some UK phone number regular expressions here.

该页面中评价最高的一个是:

The most highly rated one from that page is:

^(((+44s?d{4}|(?0d{4})?)s?d{3}s?d{3})|((+44s?d{3}|(?0d{3})?)s?d{3}s?d{4})|((+44s?d{2}|(?0d{2})?)s?d{4}s?d{4}))(s?#(d{4}|d{3}))?$

描述为:

Matches     +447222555555   | +44 7222 555 555 | (0722) 5555555 #2222
Non-Matches (+447222)555555 | +44(7222)555555  | (0722) 5555555 #22

我个人不会在电话号码上使用严格的正则表达式,除非绝对需要用户的实际电话号码进行身份验证或涉及付款的事情.电话号码有很多变化,有些用户甚至可以将 Skype 用作他们的主要电话,而通过验证您只会阻止/挫败用户.

Personally I would not put a strict regex on a phone number unless it is absolutely required to have the users actual phone number for identity verification or something involving payments. There are lots of variations of phone numbers, some users could even use Skype as their primary phone, and by putting a validation on you're just going to block/frustrate users.

另请注意,您弄错了英国格式,英国电话号码应包含 11 位数字,通常采用 (01234) 123123 格式 - 我们从不使用破折号,第一个号码应始终成为 0.

Also note you have got the UK format wrong, UK phone numbers should contain 11 digits and are normally in the format (01234) 123123 - we never use dashes and first number should always be a 0.

这篇关于基于 GB 且仅数字电话号码的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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