Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
602 views
in Technique[技术] by (71.8m points)

regex - Regular expression for GB based and only numeric phone number

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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}))?$

It is described as:

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

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...