开发者

telephone number regex

I am currently trying to validate UK telephone numbers:

The format I'm looking for is: 01234 567891 or 01234567891 - So I need the number to have 5 numbers then a space then 6 numbers or simply a 11 numbers.

The number must start with a 0.

I've had a look at a couple of examples:

开发者_开发问答

/^[0-9]{10,11} - to check that the chars are all numbers /^0[0-9]{9,10}$/ - to check that the first number is a 0

I'm just unsure how to put all these together and check if there is a space or not.

Could someone help me with this regex?

Thanks


Try this regex:

/^0\d{4}\s?\d{6}$/ 


Many people try to do input validation and formatting in a single step.

It is better to separate these processes.

Match UK telephone number in any format

^(?:(?:\(?(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?(?:\(?0\)?[\s-]?)?)|(?:\(?0))(?:(?:\d{5}\)?[\s-]?\d{4,5})|(?:\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3}))|(?:\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4})|(?:\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}))(?:[\s-]?(?:x|ext\.?|\#)\d{3,4})?$ 

The above pattern allows the user to enter the number in any format they are comfortable with. Don't constrain the user into entering specific formats.

Extract NSN, prefix and extension

^(\(?(?:0(?:0|11)\)?[\s-]?\(?|\+)(44)\)?[\s-]?)?\(?0?(?:\)[\s-]?)?([1-9]\d{1,4}\)?[\d[\s-]]+)((?:x|ext\.?|\#)\d{3,4})?$

Next, extract the various elements.

$2 will be '44' if international format was used, otherwise assume national format with leading '0'.

$4 contains the extension number if present.

$3 contains the NSN part.

Validation and formatting

Use further RegEx patterns to check the NSN has the right number of digits for this number range. Finally, store the number in E.164 format or display it in E.123 format.

There's a very detailed list of validation and display formatting RegEx patterns for UK numbers at:

http://www.aa-asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_UK_Telephone_Numbers

It's too long to reproduce here and it would be difficult to maintain multiple copies of this document.


If you are looking for all UK numbers, I'd look for a bit more than just that number, some are in the format 020 7123 4567 etc.

^\s*\(?(020[7,8]{1}\)?[ ]?[1-9]{1}[0-9{2}[ ]?[0-9]{4})|(0[1-8]{1}[0-9]{3}\)?[ ]?[1-9]{1}[0-9]{2}[ ]?[0-9]{3})\s*$


/\d*(*)*+*-*/

Simple Telephone Regex includes + () and - anywhere, as well as digits


I think ^0[\d]{4}\s?[\d]{5,6}} will work for you. I have used [\d] instead of [0-9]. I find that RegExr is a useful online tool to check and try your regular expressions. It also has a nice library of examples to help point you in the right direction


you should just count the number of digits and check that it's 10,

Some UK numbers have only 9 digits, not 10 (not including the leading 0).

These include 40 of the 01 area codes (using "4+5" format), the 016977 area code (using "5+4" format), all 0500 numbers and some 0800 numbers.

There's a list at: http://www.aa-asterisk.org.uk/index.php/01_numbers


This US numbers pattern accepts following phones as well:

800-432-4500, Opt: 9, Ext: 100316
800-432-4500, Opt: 9, Ext: X100316
800-432-4500, Option #3

(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4}),?(?:\s*(?:#|x\.?|opt(\.|:|\.:)?|option)\s*#?(\d+))?,?(?:\s*(?:#|x\.?|ext(\.|:|\.:)?|extension)\s*(\d+))?

(used this answer in other topic as start point)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜