Telephone Number Validator help

function telephoneCheck(str) {

var tel = /(\d{3}|\(\d{3}\)|1\s\d{3}|1\s\(\d{3}\)|1\(\d{3}\))(-\d{3}-|\s\d{3}-|\d{3}|\d{3}-|\s\d{3}\s)\d{4}/; 
if (tel.test(str) === true) {
  return true;
}
  else {
  return false
  };
}

telephoneCheck("555-555-5555");

How can I match the Area code which is 1

function telephoneCheck(str) {

var tel = /(\d{3}|\(\d{3}\)|1\s\d{3}|1\s\(\d{3}\)|1\(\d{3}\))(-\d{3}-|\s\d{3}-|\d{3}|\d{3}-|\s\d{3}\s)\d{4}/; 
if (tel.test(str) === true) {
  return true;
}
  else {
  return false
  };
}

telephoneCheck("2 (757) 622-7382");

it should fail this but it returning true

What have you tried so far to test for the 1 at the start?


More links



function telephoneCheck(str) {

const tel = /^(1\s)?(\d{3}|\(\d{3}\)|1\(\d{3}\))(-\d{3}-|\s\d{3}-|\d{3}|\d{3}-|\s\d{3}\s)\d{4}$/;

tel.test(str)===true ? console.log("true") : false 

};


remove the console.log and replace by true after you’re happy w the result.

Problem: you have to use ^ and $.


Edit:

There are probably more thinks to fix that you can address.

Thanks I have passed the challenge by stating the beginning with ^ and end with $

Glad you passed. Remember that you can use grouping in this way too: /(\d{3})-\1/

And will be interpreted as /\d{3}-\d{3}/ but is way simpler to write.