Guest User

Untitled

a guest
Jun 18th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. // Return true if the passed string looks like a valid US
  2. // phone number.
  3.  
  4. // The user may fill out the form field any way they choose
  5. // as long as it has the format of a valid US number. The
  6. // following are examples of valid formats for US numbers
  7. // (refer to the tests below for other variants):
  8.  
  9. // 555-555-5555 (555)555-5555 (555) 555-5555 555 555 5555
  10. // 5555555555 1 555 555 5555
  11. // For this challenge you will be presented with a string
  12. // such as 800-692-7753 or 8oo-six427676;laskdjf. Your job
  13. // is to validate or reject the US phone number based on any
  14. // combination of the formats provided above. The area code
  15. // is required. If the country code is provided, you must
  16. // confirm that the country code is 1. Return true if the
  17. // string is a valid US phone number; otherwise return false.
  18.  
  19. function telephoneCheck(str) {
  20. // Good luck!
  21. let strreg=/^(1\s?)?(\(\d{3}\)|\d{3})[\s\-]?\d{3}[\s\-]?\d{4}$/g;
  22. return !!str.match(strreg);
  23. }
  24.  
  25. telephoneCheck("555-555-5555"); // a boolean.
  26. telephoneCheck("1 555-555-5555"); // true.
  27. telephoneCheck("1 (555) 555-5555"); // true.
  28. telephoneCheck("5555555555"); // true.
  29. telephoneCheck("555-555-5555"); // true.
  30. telephoneCheck("(555)555-5555"); // true.
  31. telephoneCheck("1(555)555-5555"); // true.
  32. telephoneCheck("555-5555"); // false.
  33. telephoneCheck("5555555"); // false.
  34. telephoneCheck("1 555)555-5555"); // false.
  35. telephoneCheck("1 555 555 5555"); // true.
  36. telephoneCheck("1 456 789 4444"); // true.
  37. telephoneCheck("123**&!!asdf#"); // false.
  38. telephoneCheck("55555555"); // false.
  39. telephoneCheck("(6054756961)"); // false
  40. telephoneCheck("2 (757) 622-7382"); // false.
  41. telephoneCheck("0 (757) 622-7382"); // false.
  42. telephoneCheck("-1 (757) 622-7382"); // false
  43. telephoneCheck("2 757 622-7382"); // false.
  44. telephoneCheck("10 (757) 622-7382"); // false.
  45. telephoneCheck("27576227382"); // false.
  46. telephoneCheck("(275)76227382"); // false.
  47. telephoneCheck("2(757)6227382"); // false.
  48. telephoneCheck("2(757)622-7382"); // false.
  49. telephoneCheck("555)-555-5555"); // false.
  50. telephoneCheck("(555-555-5555"); // false.
  51. telephoneCheck("(555)5(55?)-5555"); // false.
Add Comment
Please, Sign In to add comment