18

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 then the one i need?

Thanks in advance.

yuriy636
  • 11,171
  • 5
  • 37
  • 42
Pawan Choudhary
  • 1,073
  • 1
  • 10
  • 20

6 Answers6

37

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

Community
  • 1
  • 1
genesis
  • 50,477
  • 20
  • 96
  • 125
8

Try this

function validatePhone(phoneNumber){
   var phoneNumberPattern = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;  
   return phoneNumberPattern.test(phoneNumber); 
}
ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
7

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

Silvio Troia
  • 1,439
  • 19
  • 36
3

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

this will let you validate global mobile numbers.

Aneeq Azam Khan
  • 992
  • 1
  • 10
  • 23
2

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/

Glendon G.
  • 21
  • 3
0

This regex works pretty widely for phone numbers:

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