Javascript 验证 xxx-xxx-xxxx 或 (xxx)xxx-xxxx
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13679310/
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
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 12:38:55 来源:igfitidea点击:
Validation for xxx-xxx-xxxx or (xxx)xxx-xxxx
提问by coure2011
Possible Duplicate:
A comprehensive regex for phone number validation
可能的重复:
用于电话号码验证的综合正则表达式
Whats the regex for
什么是正则表达式
xxx-xxx-xxxx
or
(xxx)xxx-xxxx
I can create regex for the first one with
我可以为第一个创建正则表达式
/^\d{3}-\d{3}-\d{4}$/
but how to add rule in this so it can also handle the second one?
但是如何在其中添加规则以便它也可以处理第二个?
回答by Rohit Jain
You can use this: -
你可以使用这个: -
/^(?:\(\d{3}\)|\d{3}-)\d{3}-\d{4}$/
回答by Daniel
I'm no guru but this should do the trick;
我不是大师,但这应该可以解决问题;
/^(\d{3}\-)?(\(\d{3}\))?\d{3}\-\d{4}$/
回答by atomAltera
/^(\(\d{3}\))|(\d{3}-)\d{3}-\d{4}$/

