# Telephone Number Validator

``````function telephoneCheck(str) {

// A phone number must have 10 digits or 11 digits if the first number is a 1
for (let i = 0; i < str.length; i++) {
if (str[i] > "0" && str[i] <= "9" && str.length() == 10 || str[i] > "0" && str[i] <= "9" && str.length() == 11 && str[0] == "1") return true;
}
// Anything else is false
return false;
}

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

Does anyone know how I can check if the str length is equal to 10 or 11? The way I am currently trying just tells me that `str.length()` is not a function. If I can find a way to fix that I think this should work, but I think it may not because things may get tricky when the phone number includes `-` or a space

Are you sure that’s the correct syntax?

str.length is not a function. Maybe skip the parentheses and try str.length. what you are trying might not work, not least because the string has dashes in it, making it longer than 10 or 11 digits. Consider using regular expressions

``````function telephoneCheck(str) {

// A phone number must have 10 digits or 11 digits if the first number is a 1
for (let i = 0; i < str.length; i++) {
if (str[i] > "0" && str[i] <= "9" && str.length == 10 || str[i] > "0" && str[i] <= "9" && str.length == 11 && str[0] == "1") return true;
}
// Anything else is false
return false;
}

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

Ahhh I see, it runs all the test now but I need to go about this in a different way. It passes all of the ones that should return false and two of the true tests but that doesn’t mean much because I am not accounting for the `-` or `()` or spaces.

``````function telephoneCheck(str) {

for (let i = 0; i < str.length; i++) {
if (str[i] > "0" && str[i] <= "9" && str.length == 11 && str[3] == "-" && str[7] == "-") {
return true;
}
if (str[i] > "0" && str[i] <= "9" && str.length == 12 && str[0] == "(" && str[4] == ")" && str[8] == "-") {
return true;
}
if (str[i] > "0" && str[i] <= "9" && str.length == 13 && str[0] == "(" && str[4] == ")" && str[5] == " " && str[9] == "-") {
return true;
}
if (str[i] > "0" && str[i] <= "9" && str.length == 11 && str[3] == " " && str[7] == " ") {
return true;
}
if (str[i] > "0" && str[i] <= "9" && str.length == 9) {
return true;
}
if (str[i] > "0" && str[i] <= "9" && str.length == 13 && str[0] == "1" && str[1] == "(" && str[5] == ")" && str[9] == "-") {
return true;
}
}

return false;
}

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

I thought this could possibly work but I was wrong. I am checking everything in the correct format examples they gave, but those aren’t passing with this code. I know there’s more I would have to check because some of the tests are unique. But I am surprised the examples in the question aren’t passing

``````function telephoneCheck(str) {

const result = str.match(/^(\\d{1}( )?)?((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}\$/);

if (str === result) {
return true
}

return false;

}

console.log(telephoneCheck("555-555-5555"));
``````

Does anyone know why this regex isn’t working?

``````function telephoneCheck(str) {

return /^(1\s|1)?(\(\d{3}\)|\d{3})(-|\s)?\d{3}(-|\s)?\d{4}\$/.test(str);
}
// starts with 1 followed by space or just 1 (? = not required)
// "(" followed by 3 digits OR just 3 digits
// followed by - OR space (? = not required) another 3 digits followed by - OR space (? = not required)
// ends with 4 digits

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

Finally got it. Regex can be really confusing, even though I got it I still don’t see how I accounted for a possible “)”…I thought I only accounted for the “(”? Can someone help me understand?

Nevermind, figured it out

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.