6

The following regular expression isn't working for international phone numbers that can allow up to 15 digits:

^[a-zA-Z0-9-().\s]{10,15}$

What needs to be adjusted?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Amen
  • 713
  • 4
  • 15
  • 28

4 Answers4

11

You may find the following regex more useful, it basically first strips all valid special characters which an international phone number can contain (spaces, parens, +, -, ., ext) and then counts if there are at least 7 digits (minimum length for a valid local number).

function isValidPhonenumber(value) {
    return (/^\d{7,}$/).test(value.replace(/[\s()+\-\.]|ext/gi, ''));
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
4

Try adding a backslash:

var unrealisticPhoneNumberRegex = /^[a-zA-Z0-9\-().\s]{10,15}$/;

Now it's still not very useful because you allow an arbitrary number of punctuation characters too. Really, validating a phone number like this — especially if you want it to really work for all possible international phone numbers — is probably a hopeless task. I suggest you go with what @BalusC suggests.

Pointy
  • 405,095
  • 59
  • 585
  • 614
0

and then counts if there are at least 7 digits (minimum length for a valid local number).

The shortest local numbers anywhere in the world are only two or three digits long.

There are many countries without area codes.

There are several well-known places with a 3 digit country code and 4 digit local numbers.

It may be prudent to drop your limit to 6 or 5; just in case.