jquery 中电话号码的正则表达式示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6960596/
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-26 21:37:03  来源:igfitidea点击:

example of a regular expression in jquery for phone numbers

jqueryregexvalidation

提问by Pawan Choudhary

I'm very new to jquery, I just want a reqular expression for validating phone numbers in jquery for one of my text field.

我对 jquery 很陌生,我只想要一个 reqular 表达式来验证 jquery 中我的一个文本字段的电话号码。

I can accept -+ () 0-9from users, do you have regex for this one or a regex for phone numbers better then the one i need?

我可以-+ () 0-9从用户那里接受,你有这个的正则表达式或电话号码的正则表达式比我需要的更好吗?

Thanks in advance.

提前致谢。

回答by genesis

Use thisrexeg

使用此正则表达式

/\(?([0-9]{3})\)?([ .-]?)([0-9]{3})([0-9]{4})/
  • (123) 456 7899
  • (123).456.7899
  • (123)-456-7899
  • 123-456-7899
  • 123 456 7899
  • 1234567899
  • (123) 456 7899
  • (123).456.7899
  • (123)-456-7899
  • 123-456-7899
  • 123 456 7899
  • 1234567899

supported

支持的

回答by ShankarSangoli

Try this

尝试这个

function validatePhone(phoneNumber){
   var phoneNumberPattern = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;  
   return phoneNumberPattern.test(phoneNumber); 
}

回答by Silvio Troia

For Italian Phone number:

意大利电话号码:

/^([+]39)?((3[\d]{2})([ ,\-,\/]){0,1}([\d, ]{6,9}))|(((0[\d]{1,4}))([ ,\-,\/]){0,1}([\d, ]{5,10}))$/
  • +39 347 12 34 567
  • 347-1234567
  • 347/1234567
  • 347 123456
  • 3471234567
  • 02/1234567
  • 051 12 34 567
  • +39 347 12 34 567
  • 347-1234567
  • 347/1234567
  • 347 123456
  • 3471234567
  • 02/1234567
  • 051 12 34 567

supported

支持的

回答by Aneeq Azam Khan

For global users have a look into this library libphonenumber-js.

对于全球用户,请查看这个库libphonenumber-js

this will let you validate global mobile numbers.

这将让您验证全球手机号码。

回答by Glendon G.

For U.S. phone numbers:

对于美国电话号码:

/(((\(\d{3}\) ?)|(\d{3}-)|(\d{3}\.))?\d{3}(-|\.)\d{4})/g; 

For U.S. hrefs with phone numbers:

对于带有电话号码的美国 hrefs:

/((\"tel:((\d{11})|(\d{10})|(((\(\d{3}\) ?)|(\d{3}-)|(\d{3}\.))?\d{3}(-|\.)\d{4}))\"))/g;

I know these are a bit clunky but they will catch most of the improperly formatted phone numbers you see around the internet.

我知道这些有点笨拙,但它们会捕获您在互联网上看到的大多数格式不正确的电话号码。

Wanna see how it works in jquery? Check out: https://jsfiddle.net/uohx1fo3/15/

想看看它在 jquery 中是如何工作的吗?退房:https: //jsfiddle.net/uohx1fo3/15/