开发者

Example of a regular expression for phone numbers

开发者 https://www.devze.com 2023-03-26 00:17 出处:网络
I\'m very new to javascript, I just want a reqular expression for validating phone numbers for one of my text field.

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

I can accept -+ () 0-9 from users, do you have regex for this one or a regex for phone numbers better the开发者_开发百科n the one i need?

Thanks in advance.


Use this rexeg

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

supported


Try this

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


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

supported


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

this will let you validate global mobile numbers.


For U.S. phone numbers:

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

For U.S. hrefs with phone numbers:

/((\"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/


This regex works pretty widely for phone numbers:

^(\+)?(\d{1,2})?[( .-]*(\d{3})[) .-]*(\d{3,4})[ .-]?(\d{4})$
0

精彩评论

暂无评论...
验证码 换一张
取 消