Rectangle 27 2

Why all those parentheses and brackets?

^(\(0\d\)\d{7}|\(02\d\)\d{6,8}|0800\s\d{5,8})$

You dont have to wrap everything in pair of parentheses to make it work. That's voodoo programming. For example, ([0][8][0][0]) is simply 0800.

This looks good and I tried it in PHP but it did not work for some reason. Anyone can help?

@Chris: All I can tell is that the expression itself works (rubular.com/r/ynr28Fcwui). You should look into the PHP docs.

php - Regular expression to validate New Zealand phone numbers - Stack...

php javascript regex
Rectangle 27 2

Why all those parentheses and brackets?

^(\(0\d\)\d{7}|\(02\d\)\d{6,8}|0800\s\d{5,8})$

You dont have to wrap everything in pair of parentheses to make it work. That's voodoo programming. For example, ([0][8][0][0]) is simply 0800.

This looks good and I tried it in PHP but it did not work for some reason. Anyone can help?

@Chris: All I can tell is that the expression itself works (rubular.com/r/ynr28Fcwui). You should look into the PHP docs.

php - Regular expression to validate New Zealand phone numbers - Stack...

php javascript regex
Rectangle 27 1

There's not enough information about the formatting of NZ phone numbers in your question to be sure about this, but a trick may be to first remove all non digit characters and after that test for the number of digits (as far as I can see it should be 9 digits?). Something like:

var phonenr = '(09)1234567'.replace(/[\(\)\s\-]/g,'')
   ,valid   = phonenr.length === 9;
// or check if it's all digits, starting with 0 as well as the length
valid = /^0\d+$/.test(phonenr) && phonenr.length === 9

Formatting of NZ phone numbers is: Mobile min: xxx xxx xxx max: xxx xxxx xxxx Landline: xx xxx xxxx OR 0800 xxx xxx 0508 xxx xxx

php - Regular expression to validate New Zealand phone numbers - Stack...

php javascript regex
Rectangle 27 1

There's not enough information about the formatting of NZ phone numbers in your question to be sure about this, but a trick may be to first remove all non digit characters and after that test for the number of digits (as far as I can see it should be 9 digits?). Something like:

var phonenr = '(09)1234567'.replace(/[\(\)\s\-]/g,'')
   ,valid   = phonenr.length === 9;
// or check if it's all digits, starting with 0 as well as the length
valid = /^0\d+$/.test(phonenr) && phonenr.length === 9

Formatting of NZ phone numbers is: Mobile min: xxx xxx xxx max: xxx xxxx xxxx Landline: xx xxx xxxx OR 0800 xxx xxx 0508 xxx xxx

php - Regular expression to validate New Zealand phone numbers - Stack...

php javascript regex
Rectangle 27 8

After reading through these answers, it looks like there wasn't a straightforward regular expression that can parse through a bunch of text and pull out phone numbers in any format (including international with and without the plus sign).

Here's what I used for a client project recently, where we had to convert all phone numbers in any format to tel: links.

/(\+*\d{1,})*([ |\(])*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4})/

PHP function to replace all phone numbers with tel: links (in case anyone is curious):

function phoneToTel($number) {
    $return = preg_replace('/(\+*\d{1,})*([ |\(])*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4})/', '<a href="tel:$1$3$4$5">$1 ($3) $4-$5</a>', $number); // includes international
    return $return;
}
+1 1234562222222222222222222222

why not just return?

A comprehensive regex for phone number validation - Stack Overflow

regex validation phone-number
Rectangle 27 8

After reading through these answers, it looks like there wasn't a straightforward regular expression that can parse through a bunch of text and pull out phone numbers in any format (including international with and without the plus sign).

Here's what I used for a client project recently, where we had to convert all phone numbers in any format to tel: links.

/(\+*\d{1,})*([ |\(])*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4})/

PHP function to replace all phone numbers with tel: links (in case anyone is curious):

function phoneToTel($number) {
    $return = preg_replace('/(\+*\d{1,})*([ |\(])*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4})/', '<a href="tel:$1$3$4$5">$1 ($3) $4-$5</a>', $number); // includes international
    return $return;
}
+1 1234562222222222222222222222

why not just return?

A comprehensive regex for phone number validation - Stack Overflow

regex validation phone-number
Rectangle 27 8

After reading through these answers, it looks like there wasn't a straightforward regular expression that can parse through a bunch of text and pull out phone numbers in any format (including international with and without the plus sign).

Here's what I used for a client project recently, where we had to convert all phone numbers in any format to tel: links.

/(\+*\d{1,})*([ |\(])*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4})/

PHP function to replace all phone numbers with tel: links (in case anyone is curious):

function phoneToTel($number) {
    $return = preg_replace('/(\+*\d{1,})*([ |\(])*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4})/', '<a href="tel:$1$3$4$5">$1 ($3) $4-$5</a>', $number); // includes international
    return $return;
}
+1 1234562222222222222222222222

why not just return?

A comprehensive regex for phone number validation - Stack Overflow

regex validation phone-number
Rectangle 27 8

After reading through these answers, it looks like there wasn't a straightforward regular expression that can parse through a bunch of text and pull out phone numbers in any format (including international with and without the plus sign).

Here's what I used for a client project recently, where we had to convert all phone numbers in any format to tel: links.

/(\+*\d{1,})*([ |\(])*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4})/

PHP function to replace all phone numbers with tel: links (in case anyone is curious):

function phoneToTel($number) {
    $return = preg_replace('/(\+*\d{1,})*([ |\(])*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4})/', '<a href="tel:$1$3$4$5">$1 ($3) $4-$5</a>', $number); // includes international
    return $return;
}
+1 1234562222222222222222222222

why not just return?

A comprehensive regex for phone number validation - Stack Overflow

regex validation phone-number
Rectangle 27 8

After reading through these answers, it looks like there wasn't a straightforward regular expression that can parse through a bunch of text and pull out phone numbers in any format (including international with and without the plus sign).

Here's what I used for a client project recently, where we had to convert all phone numbers in any format to tel: links.

/(\+*\d{1,})*([ |\(])*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4})/

PHP function to replace all phone numbers with tel: links (in case anyone is curious):

function phoneToTel($number) {
    $return = preg_replace('/(\+*\d{1,})*([ |\(])*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4})/', '<a href="tel:$1$3$4$5">$1 ($3) $4-$5</a>', $number); // includes international
    return $return;
}
+1 1234562222222222222222222222

why not just return?

A comprehensive regex for phone number validation - Stack Overflow

regex validation phone-number
Rectangle 27 8

After reading through these answers, it looks like there wasn't a straightforward regular expression that can parse through a bunch of text and pull out phone numbers in any format (including international with and without the plus sign).

Here's what I used for a client project recently, where we had to convert all phone numbers in any format to tel: links.

/(\+*\d{1,})*([ |\(])*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4})/

PHP function to replace all phone numbers with tel: links (in case anyone is curious):

function phoneToTel($number) {
    $return = preg_replace('/(\+*\d{1,})*([ |\(])*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4})/', '<a href="tel:$1$3$4$5">$1 ($3) $4-$5</a>', $number); // includes international
    return $return;
}
+1 1234562222222222222222222222

why not just return?

A comprehensive regex for phone number validation - Stack Overflow

regex validation phone-number
Rectangle 27 8

After reading through these answers, it looks like there wasn't a straightforward regular expression that can parse through a bunch of text and pull out phone numbers in any format (including international with and without the plus sign).

Here's what I used for a client project recently, where we had to convert all phone numbers in any format to tel: links.

/(\+*\d{1,})*([ |\(])*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4})/

PHP function to replace all phone numbers with tel: links (in case anyone is curious):

function phoneToTel($number) {
    $return = preg_replace('/(\+*\d{1,})*([ |\(])*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4})/', '<a href="tel:$1$3$4$5">$1 ($3) $4-$5</a>', $number); // includes international
    return $return;
}
+1 1234562222222222222222222222

why not just return?

A comprehensive regex for phone number validation - Stack Overflow

regex validation phone-number
Rectangle 27 8

After reading through these answers, it looks like there wasn't a straightforward regular expression that can parse through a bunch of text and pull out phone numbers in any format (including international with and without the plus sign).

Here's what I used for a client project recently, where we had to convert all phone numbers in any format to tel: links.

/(\+*\d{1,})*([ |\(])*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4})/

PHP function to replace all phone numbers with tel: links (in case anyone is curious):

function phoneToTel($number) {
    $return = preg_replace('/(\+*\d{1,})*([ |\(])*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4})/', '<a href="tel:$1$3$4$5">$1 ($3) $4-$5</a>', $number); // includes international
    return $return;
}
+1 1234562222222222222222222222

why not just return?

A comprehensive regex for phone number validation - Stack Overflow

regex validation phone-number
Rectangle 27 2

You can achieve this very easily.

$string = 'Main: (555) 551-0341 Pharmacy: (555) 551-4304';
preg_match_all('/(?P<name>[a-zA-Z]+): (?P<phone>[\(\)\s0-9-]{10,})/i', $string, $m);
$data  = array_combine($m['name'], $m['phone']);
$data['main']
(555) 551-0341
$data['pharmacy']

Its recommended to keep these values in an array

If you really want to put those variable in global namespace use extract funciton.

extract($data);

php - Assign Phone Numbers to their Lables with a Regular Expression -...

php regex
Rectangle 27 2

You can achieve this very easily.

$string = 'Main: (555) 551-0341 Pharmacy: (555) 551-4304';
preg_match_all('/(?P<name>[a-zA-Z]+): (?P<phone>[\(\)\s0-9-]{10,})/i', $string, $m);
$data  = array_combine($m['name'], $m['phone']);
$data['main']
(555) 551-0341
$data['pharmacy']

Its recommended to keep these values in an array

If you really want to put those variable in global namespace use extract funciton.

extract($data);

php - Assign Phone Numbers to their Lables with a Regular Expression -...

php regex
Rectangle 27 2

You can achieve this very easily.

$string = 'Main: (555) 551-0341 Pharmacy: (555) 551-4304';
preg_match_all('/(?P<name>[a-zA-Z]+): (?P<phone>[\(\)\s0-9-]{10,})/i', $string, $m);
$data  = array_combine($m['name'], $m['phone']);
$data['main']
(555) 551-0341
$data['pharmacy']

Its recommended to keep these values in an array

If you really want to put those variable in global namespace use extract funciton.

extract($data);

php - Assign Phone Numbers to their Lables with a Regular Expression -...

php regex
Rectangle 27 5

Just by looking at a phone number you can not judge whether it is valid or not, and even if it 'looks' valid it might not be the phone number you're looking for (ie. someone else's phone number).

You're trying to solve the problem at the wrong level. The only way to validate a phone number is to actually CALL it ;)

/^(1|20|21|..etc)[1-9][0-9]+$/

list of country codes (seperated by | ) followed by a digit that is not 0, followed by any digit any number of times.

You can can't do the 13-length check and the country check in one regex because the length of the country codes vary (to my knowledge at least). You can do the 13-length check by a generic regex like:

/^.{,13}$/

I second this. The OP has good and valid ideas, but there are so many ways to submit an invalid phone number that will pass any pattern check, it's not really going to have much of a point.

I just want the user to enter the phone number in a valid format. I am not worried about it being actually valid as the user will take care of that himself. Thanks for the response though! :)

@Saad: Here is an example from what I understood. Regex: (\d{2,3})(\d{4})(\d{6}) Test No: 911234567890

@Abhishek: The expression doesn't seem to work as desired. I want the expression to validate all the points mentioned in my question. Thanks for looking into it though :)

php - Regular Expression (RegEx) For Phone Numbers With Country Code C...

php regex phone-number
Rectangle 27 3

The digit immediately after the country code should not be a zero;

That system will fail for Italy. The leading 0 for Italian phone numbers is dialled from abroad.

They might be some other countries where that also happens, but Italy is the most well-known.

The maximum length of the number should not exceed 13 digits.

For shorter numbers but with an extension added, that rule will be broken in multiple countries.

With 3 digit country code, 3 digit area code and 8 digit subscriber number, you're a digit short.

php - Regular Expression (RegEx) For Phone Numbers With Country Code C...

php regex phone-number
Rectangle 27 121

I would also suggest looking at the "libphonenumber" Google Library. I know it is not regex but it does exactly what you want.

15555555555

is a possible number but not a valid number. It also supports countries outside the US.

  • Parsing/formatting/validating phone numbers for all countries/regions of the world.
  • getNumberType - gets the type of the number based on the number itself; able to distinguish Fixed-line, Mobile, Toll-free, Premium Rate, Shared Cost, VoIP and Personal Numbers (whenever feasible).
  • isNumberMatch - gets a confidence level on whether two numbers could be the same.
  • getExampleNumber/getExampleNumberByType - provides valid example numbers for all countries/regions, with the option of specifying which type of example phone number is needed.
  • isPossibleNumber - quickly guessing whether a number is a possible phonenumber by using only the length information, much faster than a full validation.
  • isValidNumber - full validation of a phone number for a region using length and prefix information.
AsYouTypeFormatter
  • findNumbers - finds numbers in text input.
PhoneNumberOfflineGeocoder

The biggest problem with phone number validation is it is very culturally dependant.

(408) 9742042
(999) 9742042
0404 999 999
(02) 9999 9999
(09) 9999 9999

not a valid

A regular expression is fine for checking the format of a phone number, but it's not really going to be able to check the validity of a phone number.

I would suggest skipping a simple regular expression to test your phone number against, and using a library such as Google's libphonenumber (link to GitHub project).

1-234-567-8901 x1234
Validation Results

Result from isPossibleNumber()  true
Result from isValidNumber()     true

Formatting Results:

E164 format                    +12345678901
Original format                (234) 567-8901 ext. 123
National format                (234) 567-8901 ext. 123
International format           +1 234-567-8901 ext. 123
Out-of-country format from US  1 (234) 567-8901 ext. 123
Out-of-country format from CH  00 1 234-567-8901 ext. 123

So not only do you learn if the phone number is valid (which it is), but you also get consistent phone number formatting in your locale.

As a bonus, libphonenumber has a number of datasets to check the validity of phone numbers, as well, so checking a number such as +61299999999 (the international version of (02) 9999 9999) returns as a valid number with formatting:

libphonenumber also gives you many additional benefits, such as grabbing the location that the phone number is detected as being, and also getting the time zone information from the phone number:

PhoneNumberOfflineGeocoder Results
Location        Australia

PhoneNumberToTimeZonesMapper Results
Time zone(s)    [Australia/Sydney]

But the invalid Australian phone number ((09) 9999 9999) returns that it is not a valid phone number.

Validation Results

Result from isPossibleNumber()  true
Result from isValidNumber()     false

Google's version has code for Java and Javascript, but people have also implemented libraries for other languages that use the Google i18n phone number dataset:

PHP

Unless you are certain that you are always going to be accepting numbers from one locale, and they are always going to be in one format, I would heavily suggest not writing your own code for this, and using libphonenumber for validating and displaying phone numbers.

When checking if it's a possible number don't you need to specify a country code? I'm using the PHP version and if I enter a British number like (replace 0's with real numbers) 07700000000 I get Missing or invalid default region. error. But if I specify the country code it will pass.

A comprehensive regex for phone number validation - Stack Overflow

regex validation phone-number
Rectangle 27 0

You can achieve this very easily.

$string = 'Main: (555) 551-0341 Pharmacy: (555) 551-4304';
preg_match_all('/(?P<name>[a-zA-Z]+): (?P<phone>[\(\)\s0-9-]{10,})/i', $string, $m);
$data  = array_combine($m['name'], $m['phone']);
$data['main']
(555) 551-0341
$data['pharmacy']

Its recommended to keep these values in an array

If you really want to put those variable in global namespace use extract funciton.

extract($data);

php - Assign Phone Numbers to their Lables with a Regular Expression -...

php regex
Rectangle 27 0

It makes not much sense to extract the variable names out of the string - if the string changes, the names would change, too. Therefore you need to make the regular expression pretty fitting to this specific case so not to introduce unexpected variables.

Also the variables you intend to extract need initialization before doing the extraction.

$string = 'Main: (555) 551-0341 Pharmacy: (555) 551-4304';

$main = $pharmacy = null;

foreach(
    preg_match_all(
        '/(Main|Pharmacy): (\(555\) 551-\d{4})/i', $string, $m, PREG_SET_ORDER
    )
    ? $m
    : []
    as $p
) {
    $p[1]  = strtolower($p[1]);
    $$p[1] = $p[2];
}

var_dump($main, $pharmacy);
string(14) "(555) 551-0341"
string(14) "(555) 551-4304"

php - Assign Phone Numbers to their Lables with a Regular Expression -...

php regex