0

I have a specific format of phone number such as

USA (+1) 000-000-0000

And I want to write a regex for this one. country code can be change but rest of the format should be same all the time. please help.

  • Possible duplicate of [Validate phone number with JavaScript](https://stackoverflow.com/questions/4338267/validate-phone-number-with-javascript) – insertusernamehere Sep 08 '17 at 09:47

2 Answers2

0

Using regex:

function validatePhone(num) {
    var re = /\b\d{3}[-.]?\d{3}[-.]?\d{4}\b/g;
    return re.test(num);
}

You might wanna build or check your own regex. based upon your format

Akarsh Satija
  • 1,756
  • 2
  • 22
  • 28
0

You can use REGEX in order to define a pattern, /^\w+ \(\+\d+\) \d{3}\-\d{3}\-\d{4}$/ will do the trick.

const pones = [
  'USA (+1) 000-000-0000',
  'USA (+12) 123-123-5656',
  'OTHERCOUNTRY (+1) 000-000-0000',
  'USA (+1) 1000-000-0000',
  'USA (+1) 000-1000-0000',
  'USA (+1) 000-000-10000',
];
const phoneRegex = /^\w+ \(\+\d+\) \d{3}\-\d{3}\-\d{4}$/

console.log(pones.map(phone => phoneRegex.test(phone)))
felixmosh
  • 32,615
  • 9
  • 69
  • 88