Skip to content

Instantly share code, notes, and snippets.

@cmcnealy
Created June 28, 2020 01:26
Show Gist options
  • Save cmcnealy/8383815eb9a1b62b66b502cf392ae2bd to your computer and use it in GitHub Desktop.
Save cmcnealy/8383815eb9a1b62b66b502cf392ae2bd to your computer and use it in GitHub Desktop.
freeCodeCamp | JavaScript Algorithms and Data Structures Projects: Telephone Number Validator: Return true if the passed string looks like a valid US phone number. The user may fill out the form field any way they choose as long as it has the format of a valid US number. The following are examples of valid formats for US numbers (refer to the te…
function telephoneCheck(str) {
// regex that matches a phone number with no spaces,
// with spaces, with dashes, and with or without country code
let regex = /^[1]*[\s|-]*\d{3}[\s|-]*\d{3}[\s|-]*\d{4}$/;
// regex that matches parentheses
// with no spaces, with spaces, with dashes,
// and with or without country code
let regexParentheses = /^[1]*[\s]*[(]\d{3}[)][\s]*\d{3}[\s|-]*\d{4}$/;
if (!regex.test(str)) {
return regexParentheses.test(str);
} else {
return true;
}
}
telephoneCheck("1 (555) 555-5555"); // returns true
telephoneCheck("1 555)555-5555") // returns false
@avalon1214
Copy link

excellent

@andreyscott
Copy link

why is the last one return false

@BlazCulina1911
Copy link

why is the last one return false

Last one is not considered valid because there is no opening bracket.

@LuisGerezM
Copy link

Para validar también el último caso deberíamos aregar: ^[1]?
Es decir el patrón quedaría así -> /^[1]?(\s)(\d{3})+[-|\s](\d{3})+[-|\s]*\d{4}$/;
Con '?' nos aseguramos que si viene el 1, sea 1 vez.

@HancockJ
Copy link

Got it in 1 line if anyone is interested:
return /^(1 |1)?((\d{3})|(\(\d{3}\)))(-| |)(\d{3})(-| |)(\d{4})$/.test(str)

@MahasSerhii
Copy link

Another way:

function telephoneCheck(str) {
let regex = /^(1 |1)?((\d{3})|((\d{3})))(-| |)(\d{3})(-| |)(\d{4})$/;
return (regex.test(str)==true)
}
console.log(telephoneCheck("1 (555) 555-5555")) // returns true
console.log(telephoneCheck("1 555)555-5555")) // returns false

@RihardXXX
Copy link

Got it in 1 line if anyone is interested: return /^(1 |1)?((\d{3})|(\(\d{3}\)))(-| |)(\d{3})(-| |)(\d{4})$/.test(str)

very very good^ super

@M3ntalist
Copy link

Got it in 1 line if anyone is interested: return /^(1 |1)?((\d{3})|(\(\d{3}\)))(-| |)(\d{3})(-| |)(\d{4})$/.test(str)

very very good^ super

@RihardXXX Thanks for that, I was going the long route.

@huuhoang2000
Copy link

Got it in 1 line if anyone is interested:
return /^(1 |1)?((\d{3})|((\d{3})))(-| |)(\d{3})(-| |)(\d{4})$/.test(str)

You are incredible.

@PrithvirajSawant
Copy link

alternative:``

function telephoneCheck(str) {
  let regex = /^(1\s?)?(\(\d{3}\)|\d{3})([\s\-]?)\d{3}([\s\-]?)\d{4}$/;
  return regex.test(str);
}

console.log(telephoneCheck("1 (555) 555-5555")); // returns true
console.log(telephoneCheck("1 555)555-5555")); // returns false

@tikhepooja11
Copy link

return /^(1 |1)?((\d{3})|((\d{3})))(-| |)(\d{3})(-| |)(\d{4})$/.test(str)

Thanks a lot...your supperb.

@kamate-Idris
Copy link

Got it in 1 line if anyone is interested: return /^(1 |1)?((\d{3})|(\(\d{3}\)))(-| |)(\d{3})(-| |)(\d{4})$/.test(str)

Wonderfully done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment