最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - Javascript Regular Expression for International Phone Number - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question edited Nov 10, 2010 at 18:16 BalusC 1.1m376 gold badges3.6k silver badges3.6k bronze badges asked Nov 10, 2010 at 18:13 AmenAmen 7134 gold badges15 silver badges28 bronze badges 4
  • 1 And you are sure that "international phone numbers" are not longer than 15 digits? – Tomalak Commented Nov 10, 2010 at 18:15
  • I am just following the instructions of my client. – Amen Commented Nov 10, 2010 at 18:18
  • Then what are the rules that your client has instructed you to follow? – The Archetypal Paul Commented Nov 10, 2010 at 18:19
  • what that {10,15} means anyway? is that minimum is 10 digit and max is 15 digits? – gumuruh Commented Sep 14, 2019 at 15:36
Add a comment  | 

4 Answers 4

Reset to default 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, ''));
}

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.

See A comprehensive regex for phone number validation and Is there a standard for storing normalized phone numbers in a database?

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.

发布评论

评论列表(0)

  1. 暂无评论