javascript 验证电话号码的美国格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5841888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
validate american format of phone number
提问by Awais Qarni
I want to validate the phone and fax number fields. I want to validate them as phone numbers and fax are given in American format. I have searched but not succeeded. Any one can help how can I validate in following format
我想验证电话和传真号码字段。我想验证它们,因为电话号码和传真以美国格式给出。我已经搜索过但没有成功。任何人都可以帮助我如何以以下格式进行验证
Edit
1 (415) xxx xxxx
Edit
1 (415) xxx xxxx
回答by Random832
The best way is to remove all non-digits and then format it into your preferred format yourself.
最好的方法是删除所有非数字,然后自己将其格式化为您喜欢的格式。
var raw_number = str.replace(/[^0-9]/g,'');
var regex1 = /^1?([2-9]..)([2-9]..)(....)$/;
if(!regex1.test(raw_number)) {
// is invalid...
} else {
var formatted_number = str.replace(regex1,'1 () ')
}
That way if they enter 234/555-0123
it will become your preferred format of 1 (234) 555 0123
.
这样,如果他们输入234/555-0123
,它将成为您首选的1 (234) 555 0123
.
回答by Billbad
The following Javascript REGEX validates 123-456-7890 or 123.456.7890 or 1234567890 or 123 456 7890 or (123) 456-7890
以下 Javascript REGEX 验证 123-456-7890 或 123.456.7890 或 1234567890 或 123 456 7890 或 (123) 456-7890
^[(]{0,1}[0-9]{3}[)]{0,1}[-\s\.]{0,1}[0-9]{3}[-\s\.]{0,1}[0-9]{4}$
回答by Eric Redon
Using regular expressions is fine, I guess, if you are conscienciously limiting your feature to US-formed phone numbers.
使用正则表达式很好,我想,如果您有意识地将您的功能限制为美国格式的电话号码。
However as soon as your code has to deal with international, that method is no longer the most appropriate option. If you have such plans of any kind, and since you are using JavaScript, I suggest that you have a look at Google's libphonenumber.
但是,一旦您的代码必须处理国际问题,该方法就不再是最合适的选择。如果您有任何此类计划,并且由于您使用的是 JavaScript,我建议您查看 Google 的libphonenumber。
回答by AlBeebe
PhoneFormat.comhas a javascript library that makes formatting and validating numbers really easy.
PhoneFormat.com有一个 javascript 库,可以让格式化和验证数字变得非常容易。
回答by Jared Farrish
Here is one I use (jQuery) for a field validation onBlur:
这是我使用 (jQuery) 进行 onBlur 字段验证的一个:
http://jsfiddle.net/userdude/Ju72W/
http://jsfiddle.net/userdude/Ju72W/
jQuery(document).ready(function($){
$cf = $('#customfield_10117');
$cf.blur(function(e){
phone = $(this).val();
phone = phone.replace(/[^0-9]/g,'');
if (phone.length != 10) {
e.preventDefault();
if (confirm('Phone number must include area code and prefix.')) {
setTimeout(function(){$cf.focus()}, 0);
}
} else {
area = phone.substring(0,3);
prefix = phone.substring(3,6);
line = phone.substring(6);
$(this).val('(' + area + ') ' + prefix + '-' + line);
}
});
});
It checks if there were 10 numbers submitted, and then if that is true, reformats to (000) 000-0000 format.
它检查是否提交了 10 个数字,如果是,则将其重新格式化为 (000) 000-0000 格式。
EDIT
编辑
A function that uses the same technique (with an added country code qualifier).
使用相同技术的函数(添加了国家/地区代码限定符)。
http://jsfiddle.net/userdude/Ju72W/1/
http://jsfiddle.net/userdude/Ju72W/1/
jQuery(document).ready(function($){
$cf = $('#customfield_10117');
$cf.blur(function(e){
number = phoneCheckAndFormat(this.value, 11);
if (number === false) {
alert('Entered phone number is not correct.');
return;
}
$(this).val(number);
});
});
function phoneCheckAndFormat(phone, digits) {
phone = phone.replace(/[^0-9]/g,'');
digits = (digits > 0 ? digits : 10);
if (phone.length != digits) {
return false;
} else {
code = '';
if (digits == 11) {
code = '1 ';
phone = phone.substring(1);
}
area = phone.substring(0,3);
prefix = phone.substring(3,6);
line = phone.substring(6);
return code + '(' + area + ') ' + prefix + '-' + line;
}
}