This question already has an answer here:
这个问题已经有了答案:
- What regular expression will match valid international phone numbers? 20 answers
- 什么正则表达式将匹配有效的国际电话号码?20个答案
I use Clickatell to send SMSes to clients' mobile phones.
我使用Clickatell向客户的手机发送SMSes。
Is there a standardised regular expression for all valid mobile phone numbers, e.g. +27 123 4567? I'd roll my own, but I'm worried about missing an obscure, valid phone number format.
是否有一个标准的正则表达式来表示所有有效的手机号码,例如+ 271234567 ?我自己也会滚,但我担心错过一个模糊的、有效的电话号码格式。
6 个解决方案
#1
21
After stripping all characters except '+' and digits from your input, this should do it:
在从输入中去掉除'+'和数字之外的所有字符后,应该这样做:
^\+[1-9]{1}[0-9]{3,14}$
If you want to be more exact with the country codes see this question on List of phone number country codes
如果你想更准确地使用国家代码,请在电话号码国家代码清单上看到这个问题。
However, I would try to be not too strict with my validation. Users get very frustrated if they are told their valid numbers are not acceptable.
但是,我尽量不要对我的认可太严格。如果用户被告知他们的有效号码是不可接受的,他们会非常沮丧。
#2
5
Even if you write a regular expression that matches exactly the subset "valid phone numbers" out of strings, there is no way to guarantee (by way of a regular expression) that they are valid mobile phone numbers. In several countries, mobile phone numbers are indistinguishable from landline phone numbers without at least a number plan lookup, and in some cases, even that won't help. For example, in Sweden, lots of people have "ported" their regular, landline-like phone number to their mobile phone. It's still the same number as they had before, but now it goes to a mobile phone instead of a landline.
即使您编写的正则表达式与字符串中的“有效电话号码”子集完全匹配,也无法(通过正则表达式)保证它们是有效的手机号码。在一些国家,手机号码与固定电话号码是不可区分的,没有至少一个数字计划查找,在某些情况下,即使这样也无济于事。例如,在瑞典,很多人都把他们的固定电话号码“移植”到他们的手机上。它仍然和以前一样,但是现在它变成了移动电话而不是固定电话。
Since valid phone numbers consist only of digits, I doubt that rolling your own would risk missing some obscure case of phone number at least. If you want to have better certainty, write a generator that takes a list of all valid country codes, and requires one of them at the beginning of the phone number to be matched by the generated regular expression.
由于有效的电话号码只包含数字,我怀疑至少滚动你自己的电话号码可能会丢失一些模糊的电话号码。如果您希望有更好的确定性,那么编写一个生成器,它获取所有有效的国家代码的列表,并要求在电话号码的开头使用其中一个代码与生成的正则表达式进行匹配。
#3
2
^\+[1-9]{1}[0-9]{7,11}$
The Regular Expression ^\+[1-9]{1}[0-9]{7,11}$
fails for "+290 8000" and similar valid numbers that are shorter than 8 digits.
正则表达式^ \ +(1 - 9){ 1 }[0 - 9]{ 7、11 }失败美元“290 + 290”和类似的有效数字,短于8位数。
The longest numbers could be something like 3 digit country code, 3 digit area code, 8 digit subscriber number, making 14 digits.
最长的数字可以是3位数的国家代码,3位数的地区代码,8位数的用户号,14位数。
#4
2
Even though it is about international numbers I would want the code to be like :
即使是关于国际号码的,我也希望代码是这样的:
/^(\+|\d)[0-9]{7,16}$/;
As you can have international numbers starting with '00' as well.
你也可以用“00”开头的国际号码。
Why I prefer 15 digits : http://en.wikipedia.org/wiki/E.164
为什么我喜欢15位数字:http://en.wikipedia.org/wiki/E.164 ?
#5
2
Posting a note here for users looking into this into the future. Google's libphonenumber
is what you most likely would want to use. There is wrappers for PHP, node.js, Java, etc. to use the data which Google has been collecting and reduces the requirements for maintaining large arrays of regex patterns to apply.
在这里为关注未来的用户发布一条消息。谷歌的libphonenumber是您最希望使用的。有PHP、node的包装。使用谷歌一直在收集的数据,并减少了维护regex模式大数组的需求。
#6
0
// Regex - Check Singapore valid mobile numbers
// Regex -检查新加坡有效手机号码
public static boolean isSingaporeMobileNo(String str) {
Pattern mobNO = Pattern.compile("^(((0|((\\+)?65([- ])?))|((\\((\\+)?65\\)([- ])?)))?[8-9]\\d{7})?$");
Matcher matcher = mobNO.matcher(str);
if (matcher.find()) {
return true;
} else {
return false;
}
}
#1
21
After stripping all characters except '+' and digits from your input, this should do it:
在从输入中去掉除'+'和数字之外的所有字符后,应该这样做:
^\+[1-9]{1}[0-9]{3,14}$
If you want to be more exact with the country codes see this question on List of phone number country codes
如果你想更准确地使用国家代码,请在电话号码国家代码清单上看到这个问题。
However, I would try to be not too strict with my validation. Users get very frustrated if they are told their valid numbers are not acceptable.
但是,我尽量不要对我的认可太严格。如果用户被告知他们的有效号码是不可接受的,他们会非常沮丧。
#2
5
Even if you write a regular expression that matches exactly the subset "valid phone numbers" out of strings, there is no way to guarantee (by way of a regular expression) that they are valid mobile phone numbers. In several countries, mobile phone numbers are indistinguishable from landline phone numbers without at least a number plan lookup, and in some cases, even that won't help. For example, in Sweden, lots of people have "ported" their regular, landline-like phone number to their mobile phone. It's still the same number as they had before, but now it goes to a mobile phone instead of a landline.
即使您编写的正则表达式与字符串中的“有效电话号码”子集完全匹配,也无法(通过正则表达式)保证它们是有效的手机号码。在一些国家,手机号码与固定电话号码是不可区分的,没有至少一个数字计划查找,在某些情况下,即使这样也无济于事。例如,在瑞典,很多人都把他们的固定电话号码“移植”到他们的手机上。它仍然和以前一样,但是现在它变成了移动电话而不是固定电话。
Since valid phone numbers consist only of digits, I doubt that rolling your own would risk missing some obscure case of phone number at least. If you want to have better certainty, write a generator that takes a list of all valid country codes, and requires one of them at the beginning of the phone number to be matched by the generated regular expression.
由于有效的电话号码只包含数字,我怀疑至少滚动你自己的电话号码可能会丢失一些模糊的电话号码。如果您希望有更好的确定性,那么编写一个生成器,它获取所有有效的国家代码的列表,并要求在电话号码的开头使用其中一个代码与生成的正则表达式进行匹配。
#3
2
^\+[1-9]{1}[0-9]{7,11}$
The Regular Expression ^\+[1-9]{1}[0-9]{7,11}$
fails for "+290 8000" and similar valid numbers that are shorter than 8 digits.
正则表达式^ \ +(1 - 9){ 1 }[0 - 9]{ 7、11 }失败美元“290 + 290”和类似的有效数字,短于8位数。
The longest numbers could be something like 3 digit country code, 3 digit area code, 8 digit subscriber number, making 14 digits.
最长的数字可以是3位数的国家代码,3位数的地区代码,8位数的用户号,14位数。
#4
2
Even though it is about international numbers I would want the code to be like :
即使是关于国际号码的,我也希望代码是这样的:
/^(\+|\d)[0-9]{7,16}$/;
As you can have international numbers starting with '00' as well.
你也可以用“00”开头的国际号码。
Why I prefer 15 digits : http://en.wikipedia.org/wiki/E.164
为什么我喜欢15位数字:http://en.wikipedia.org/wiki/E.164 ?
#5
2
Posting a note here for users looking into this into the future. Google's libphonenumber
is what you most likely would want to use. There is wrappers for PHP, node.js, Java, etc. to use the data which Google has been collecting and reduces the requirements for maintaining large arrays of regex patterns to apply.
在这里为关注未来的用户发布一条消息。谷歌的libphonenumber是您最希望使用的。有PHP、node的包装。使用谷歌一直在收集的数据,并减少了维护regex模式大数组的需求。
#6
0
// Regex - Check Singapore valid mobile numbers
// Regex -检查新加坡有效手机号码
public static boolean isSingaporeMobileNo(String str) {
Pattern mobNO = Pattern.compile("^(((0|((\\+)?65([- ])?))|((\\((\\+)?65\\)([- ])?)))?[8-9]\\d{7})?$");
Matcher matcher = mobNO.matcher(str);
if (matcher.find()) {
return true;
} else {
return false;
}
}