Created
January 24, 2016 03:06
-
-
Save WebDevNoob/6880ff3beb0f69225bee to your computer and use it in GitHub Desktop.
For this challenge you will be presented with a string such as 800-692-7753 or 8oo-six427676;laskdjf. Your job is to validate or reject the US phone number based on any combination of the formats provided above. The area code is required. If the country code is provided, you must confirm that the country code is 1. Return true if the string is a…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function telephoneCheck(str) { | |
// if str starts with a dash, return false, else replace dashes and spaces with nothing | |
if (str[0] == "-"){ | |
return false; | |
}else | |
str = str.replace(/-|\s/g, ""); | |
//Location of first parentheses | |
var firstPar = str.search("\\("); | |
//Location of second parentheses | |
var secondPar = str.search("\\)"); | |
//Length of the str | |
var strLen = str.length; | |
//Depending on string length | |
switch(strLen){ | |
case 10: | |
return true; | |
case 11: | |
if (str[0] == 1){ | |
return true; | |
}else | |
return false; | |
break; | |
case 12: | |
if (str[0] === "(" && str[4] == ")"){ | |
return true; | |
}else | |
return false; | |
break; | |
case 13: | |
if (str[0] == 1 && firstPar === 1 && secondPar === 5){ | |
return true; | |
}else | |
return false; | |
break; | |
default: | |
return false; | |
} | |
} | |
telephoneCheck("555-555-5555"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
telephoneCheck("55 55-55-555-5") should return false.