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: