Skip to content

Instantly share code, notes, and snippets.

@Shaikot007
Last active July 12, 2021 11:27
Show Gist options
  • Save Shaikot007/9504b94a5bc98958b57f242cb436e0aa to your computer and use it in GitHub Desktop.
Save Shaikot007/9504b94a5bc98958b57f242cb436e0aa to your computer and use it in GitHub Desktop.
Telephone number validator. (freeCodeCamp JavaScript algorithms and data structures challenge)
function telephoneCheck(str) {
var regex1 = /^[0-9]{3}[-][0-9]{3}[-][0-9]{4}$/;
var regex2 = /^[(][0-9]{3}[)][0-9]{3}[-][0-9]{4}$/;
var regex3 = /^[(][0-9]{3}[)][\s][0-9]{3}[-][0-9]{4}$/;
var regex4 = /^[0-9]{3}[\s][0-9]{3}[\s][0-9]{4}$/;
var regex5 = /^[0-9]{10}$/;
var regex6 = /^[1]{1}[\s][0-9]{3}[\s][0-9]{3}[\s][0-9]{4}$/;
var regex7 = /^[1]{1}[\s][0-9]{3}[-][0-9]{3}[-][0-9]{4}$/;
var regex8 = /^[1]{1}[\s][(][0-9]{3}[)][\s][0-9]{3}[-][0-9]{4}$/;
var regex9 = /^[1]{1}[(][0-9]{3}[)][0-9]{3}[-][0-9]{4}$/;
if (str.match(regex1)) {
return true;
}
else if (str.match(regex2)) {
return true;
}
else if (str.match(regex3)) {
return true;
}
else if (str.match(regex4)) {
return true;
}
else if (str.match(regex5)) {
return true;
}
else if (str.match(regex6)) {
return true;
}
else if (str.match(regex7)) {
return true;
}
else if (str.match(regex8)) {
return true;
}
else if (str.match(regex9)) {
return true;
}
else {
return false;
}
};
telephoneCheck("555-555-5555")
// should return a boolean.
// 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.
// telephoneCheck("555-5555")
// should return false.
// telephoneCheck("5555555")
// should return false.
// telephoneCheck("1 555)555-5555")
// should return false.
// telephoneCheck("1 555 555 5555")
// should return true.
// telephoneCheck("1 456 789 4444")
// should return true.
// telephoneCheck("123**&!!asdf#")
// should return false.
// telephoneCheck("55555555")
// should return false.
// telephoneCheck("(6054756961)")
// should return false
// telephoneCheck("2 (757) 622-7382")
// should return false.
// telephoneCheck("0 (757) 622-7382")
// should return false.
// telephoneCheck("-1 (757) 622-7382")
// should return false
// telephoneCheck("2 757 622-7382")
// should return false.
// telephoneCheck("10 (757) 622-7382")
// should return false.
// telephoneCheck("27576227382")
// should return false.
// telephoneCheck("(275)76227382")
// should return false.
// telephoneCheck("2(757)6227382")
// should return false.
// telephoneCheck("2(757)622-7382")
// should return false.
// telephoneCheck("555)-555-5555")
// should return false.
// telephoneCheck("(555-555-5555")
// should return false.
// telephoneCheck("(555)5(55?)-5555")
// should return false.
// 555-555-5555
// (555)555-5555
// (555) 555-5555
// 555 555 5555
// 5555555555
// 1 555 555 5555
// freeCodeCamp javaScript algorithms and data structures challenge link is given below:
// https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/telephone-number-validator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment