Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. /*JavaScript Algorithms and Data Structures Projects: Telephone Number Validator:
  2. Challenge designed by FreeCodeCamp, solution derived from learning obtained thanks to FreeCodeCamp and with
  3. some assistance from other people's solutions on the internet. I'm not willing to take ownership of the below
  4. code beyond that I was on the right track. ie. completely my solution however forgot about the ? syntax as an
  5. option and therefore followed someone elses example to implement this into my solution. Once I figured out how
  6. it worked for the first part of the regular expression I debugged the remainder myself and implemented the ?
  7. regex syntax without following any examples.
  8. Function calls provided by FreeCodeCamp as ways to test the algorithm*/
  9.  
  10. function telephoneCheck(str) {
  11. var r1 = /^(1\s?)?(\d{3}-\d{3}-\d{4}$|[(]\d{3}[)](\s?)\d{3}-\d{4}$|^[(]\d{3}[)] \d{3}-\d{4}$|^\d{3} \d{3} \d{4}$|^\d{10}$|^\d{1} \d{3} \d{3} \d{4}$)/;
  12.  
  13. console.log(str + ": "+ r1.test(str));
  14. if (r1.test(str)) {
  15. return true;
  16. } else {
  17. return false;
  18. }
  19. }
  20.  
  21. telephoneCheck("555-555-5555") //should return a boolean.
  22. telephoneCheck("1 555-555-5555") //should return true.
  23. telephoneCheck("1 (555) 555-5555") //should return true.
  24. telephoneCheck("5555555555") //should return true.
  25. telephoneCheck("555-555-5555") //should return true.
  26. telephoneCheck("(555)555-5555") //should return true.
  27. telephoneCheck("1(555)555-5555") //should return true.
  28. telephoneCheck("555-5555") //should return false.
  29. telephoneCheck("5555555") //should return false.
  30. telephoneCheck("1 555)555-5555") //should return false.
  31. telephoneCheck("1 555 555 5555") //should return true.
  32. telephoneCheck("1 456 789 4444") //should return true.
  33. telephoneCheck("123**&!!asdf#") //should return false.
  34. telephoneCheck("55555555") //should return false.
  35. telephoneCheck("(6054756961)") //should return false
  36. telephoneCheck("2 (757) 622-7382") //should return false.
  37. telephoneCheck("0 (757) 622-7382") //should return false.
  38. telephoneCheck("-1 (757) 622-7382") //should return false
  39. telephoneCheck("2 757 622-7382") //should return false.
  40. telephoneCheck("10 (757) 622-7382") //should return false.
  41. telephoneCheck("27576227382") //should return false.
  42. telephoneCheck("(275)76227382") //should return false.
  43. telephoneCheck("2(757)6227382") //should return false.
  44. telephoneCheck("2(757)622-7382") //should return false.
  45. telephoneCheck("555)-555-5555") //should return false.
  46. telephoneCheck("(555-555-5555") //should return false.
  47. telephoneCheck("(555)5(55?)-5555") //should return false.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement