Pawan Choudhary
Pawan Choudhary

Reputation: 1083

Example of a regular expression for phone numbers

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.

Upvotes: 18

Views: 96292

Answers (6)

Liron Dadon
Liron Dadon

Reputation: 61

This regex works pretty widely for phone numbers:

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

Upvotes: 0

Aneeq Azam Khan
Aneeq Azam Khan

Reputation: 1032

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

this will let you validate global mobile numbers.

Upvotes: 3

Glendon G.
Glendon G.

Reputation: 21

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/

Upvotes: 2

Silvio Troia
Silvio Troia

Reputation: 1459

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

Upvotes: 7

ShankarSangoli
ShankarSangoli

Reputation: 69915

Try this

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

Upvotes: 8

genesis
genesis

Reputation: 50982

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

Upvotes: 37

Related Questions