Created
December 28, 2018 14:22
-
-
Save jmaicaaan/d5a3243fde02c508f19040a1b0099077 to your computer and use it in GitHub Desktop.
FCC - JavaScript Algorithms and Data Structures Projects: Telephone Number Validator
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(phoneNumber = "") { | |
const hasValidLength = isValidTelephoneLength(phoneNumber); | |
let copyPhoneNumber = phoneNumber; | |
if (!hasValidLength) return false; | |
if (hasCountryCode(copyPhoneNumber)) { | |
if (!hasValidCountryCode(copyPhoneNumber)) return false; | |
copyPhoneNumber = cleanWhitespaceAtStart( | |
copyPhoneNumber.substring(1, copyPhoneNumber.length) | |
); | |
} else { | |
// check if its straight 10 length | |
// eg: 5555555555 | |
if (copyPhoneNumber.length === 10) return true; | |
} | |
if (containValidPrefixNumberParentheses(copyPhoneNumber)) { | |
return isValidPhoneNumberValidAfterPrefixNumber(copyPhoneNumber); | |
} | |
return ( | |
containValidPhoneNumberSeparatorAndParts(copyPhoneNumber, "-") || | |
containValidPhoneNumberSeparatorAndParts(copyPhoneNumber, " ") | |
); | |
} | |
function getPhoneNumberLength(phoneNumber = "") { | |
const numberRegex = /\d+/g; | |
return phoneNumber.match(numberRegex).join("").length; | |
} | |
function isValidTelephoneLength(phoneNumber = "") { | |
const acceptedTelephoneLengths = [10, 11]; | |
const phoneNumberLength = getPhoneNumberLength(phoneNumber); | |
return acceptedTelephoneLengths.indexOf(phoneNumberLength) > -1; | |
} | |
function hasCountryCode(phoneNumber = "") { | |
return getPhoneNumberLength(phoneNumber) === 11 ? true : false; | |
} | |
function hasValidCountryCode(phoneNumber = "") { | |
const acceptedCountryCodes = [1]; | |
const firstNumber = parseInt(phoneNumber[0]); | |
return acceptedCountryCodes.indexOf(firstNumber) > -1; | |
} | |
// A valid parentheses appears for the first 3 digits of the phoneNumber | |
// !!! Note that this may difer if the phoneNumber has a country code !!! | |
function containValidPrefixNumberParentheses(phoneNumber = "") { | |
const matchCapture = phoneNumber.substring(0, 5); | |
// Check if the first and last is ( or ) | |
if ( | |
matchCapture[0] === "(" && | |
matchCapture[matchCapture.length - 1] === ")" | |
) { | |
return true; | |
} | |
return false; | |
} | |
function isValidPhoneNumberValidAfterPrefixNumber(phoneNumber = "") { | |
// eg: (555)555-5555 | |
// we get the "555-5555" and check if their separator is valid '-' | |
// There should only one group of ( ) | |
// example of invalid: (555)5(55?)-5555 | |
const [parenthesesIndexA, parenthesesIndexB] = [ | |
phoneNumber.indexOf(")"), | |
phoneNumber.lastIndexOf(")") | |
]; | |
if (parenthesesIndexA !== parenthesesIndexB) return false; | |
const indexAfterLastParentheses = phoneNumber.lastIndexOf(")"); | |
const telephoneNumberWithoutPrefix = phoneNumber.substring( | |
indexAfterLastParentheses + 1, | |
phoneNumber.length | |
); | |
return telephoneNumberWithoutPrefix.split("-").length === 2; | |
} | |
function containValidPhoneNumberSeparatorAndParts( | |
phoneNumber = "", | |
separator = "-" | |
) { | |
const phoneNumberParts = phoneNumber.split(separator); | |
phoneNumber; | |
if (phoneNumberParts.length === 3) { | |
// first part length = 3; second part length = 3; third part length = 4 | |
phoneNumberParts; | |
return ( | |
phoneNumberParts[0].length === 3 && | |
phoneNumberParts[1].length === 3 && | |
phoneNumberParts[2].length === 4 | |
); | |
} | |
return false; | |
} | |
function cleanWhitespaceAtStart(phoneNumber = "") { | |
return phoneNumber.replace(/^\s+(?=\d)/g, "").replace(/^\s+(?=\W)/g, ""); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment