Needs some help with the telephone validator

Tell us what’s happening:
In my code or logic, I use the REGEXP to validate the phone number with the exception of the country code. So created a character array to test for country code one then strip the the one and validate the number. Ther are so tests that are not passing through correctly.
these are
telephoneCheck("1 555-555-5555") should return true.

telephoneCheck("1 (555) 555-5555") should return true.

telephoneCheck("5555555555") should return true.

telephoneCheck("555-555-5555") should return true.

telephoneCheck("(555)555-5555") should return true.

telephoneCheck("1(555)555-5555") should return true.

Your code so far


function telephoneCheck(str) {
 var phone = str.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"").replace(/\s/g,"");

 var phonere = new RegExp(/^\(?([0-9]{3})\)?[-.() ]?([0-9]{3})[-.() ]?([0-9]{4})$/gm);
if(phone.length >= 10) { 
 var array = phone.split('');
 if (array[0] == 1) {
    phone = array.slice(1);
    phone.join();
  //console.log(" the 1 is validated and removed " + phone)}
 }
}

 console.log(phone);//
 console.log(phonere.test(phone));
 return phonere.test(phone);
}

telephoneCheck("(555)555-5555");
telephoneCheck("1 (555)555-5555");
telephoneCheck("1 555 555 5555");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 Edg/85.0.564.70.

Challenge: Telephone Number Validator

Link to the challenge:

Hello @lisardean82!

Here in my PC, this code is not passing on tease testes:

telephoneCheck("1 555-555-5555") should return true.
telephoneCheck("1 (555) 555-5555") should return true.
telephoneCheck("1(555)555-5555") should return true.
telephoneCheck("1 555 555 5555") should return true.
telephoneCheck("1 456 789 4444") should return true.
telephoneCheck("(6054756961)") should return false
telephoneCheck("555)-555-5555") should return false.
telephoneCheck("(555-555-5555") should return false. 

In your first var, you are trying to match all characters that are not number in the world, this is a very hard thing to do, why don’t you try something more simple, like:

var phone = str.replace(/[^0-9]/g,"").replace(/\s/g,"");

And after this point, you can check if the numbers are OK, if they are, you start checking the especial characters.

Here is a Regex validator:

In my opinion, it is very hard to solve this problem only with Regex, but it is possible. When I solved it, I made a large sequency of ifs.

I know that is not much, but I holpe that I helped you with somehow

Since the country code and its space separator are optional and there is only zero or one of them, you might want to try a group at the beginning of your regular expression that matches the country code and use the ? operator.

The rest of the test problems appear to be with the location and balance of the parentheses for the area code.

This has optional parentheses around the area code (\(?([0-9]{3})\)?), but you could match just one or just the other. You really need an regular expression that matches (123) or 123, so you either match both parentheses on the area code or none. Try a (...|...) group. According to the format given in the problem specification, you don’t need parentheses elsewhere, so you may want to modify your separator regular expressions [-.() ]?.