Reputation: 1083
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
Reputation: 61
This regex works pretty widely for phone numbers:
^(\+)?(\d{1,2})?[( .-]*(\d{3})[) .-]*(\d{3,4})[ .-]?(\d{4})$
Upvotes: 0
Reputation: 1032
For global users have a look into this library libphonenumber-js.
this will let you validate global mobile numbers.
Upvotes: 3
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
Reputation: 1459
For Italian Phone number:
/^([+]39)?((3[\d]{2})([ ,\-,\/]){0,1}([\d, ]{6,9}))|(((0[\d]{1,4}))([ ,\-,\/]){0,1}([\d, ]{5,10}))$/
supported
Upvotes: 7
Reputation: 69915
Try this
function validatePhone(phoneNumber){
var phoneNumberPattern = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;
return phoneNumberPattern.test(phoneNumber);
}
Upvotes: 8