智慧水务管理系统 - 精河县供水工程综合管理平台

phone-number-utils.js 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Regex that specifies any delimiter char that allows us to treat the number as
  2. // a phone number rather than just any other number that could appear in text.
  3. var hasDelimCharsRe = /[-. ()]/;
  4. // Over the years, many people have added to this regex, but it should have been
  5. // split up by country. Maybe one day we can break this down.
  6. var mostPhoneNumbers = /(?:(?:(?:(\+)?\d{1,3}[-. ]?)?\(?\d{3}\)?[-. ]?\d{3}[-. ]?\d{4})|(?:(\+)(?:9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7|1)[-. ]?(?:\d[-. ]?){6,12}\d+))([,;]+[0-9]+#?)*/;
  7. // Regex for Japanese phone numbers
  8. var japanesePhoneRe = /(0([1-9]-?[1-9]\d{3}|[1-9]{2}-?\d{3}|[1-9]{2}\d{1}-?\d{2}|[1-9]{2}\d{2}-?\d{1})-?\d{4}|0[789]0-?\d{4}-?\d{4}|050-?\d{4}-?\d{4})/;
  9. // Combined regex
  10. var validPhoneNumberRe = new RegExp("^".concat(mostPhoneNumbers.source, "|").concat(japanesePhoneRe.source, "$"));
  11. /**
  12. * Determines if the character is a phone number separator character (i.e.
  13. * '-', '.', or ' ' (space))
  14. */
  15. export function isPhoneNumberSeparatorChar(charCode) {
  16. return (charCode === 45 /* Char.Dash */ || // '-'
  17. charCode === 46 /* Char.Dot */ || // '.'
  18. charCode === 32 /* Char.Space */ // ' '
  19. );
  20. }
  21. /**
  22. * Determines if the character is a control character in a phone number. Control
  23. * characters are as follows:
  24. *
  25. * - ',': A 1 second pause. Useful for dialing extensions once the main phone number has been reached
  26. * - ';': A "wait" that waits for the user to take action (tap something, for instance on a smart phone)
  27. */
  28. export function isPhoneNumberControlChar(charCode) {
  29. return (charCode === 44 /* Char.Comma */ || // ','
  30. charCode === 59 /* Char.SemiColon */ // ';'
  31. );
  32. }
  33. /**
  34. * Determines if the given phone number text found in a string is a valid phone
  35. * number.
  36. *
  37. * Our state machine parser is simplified to grab anything that looks like a
  38. * phone number, and this function confirms the match.
  39. */
  40. export function isValidPhoneNumber(phoneNumberText) {
  41. // We'll only consider the match as a phone number if there is some kind of
  42. // delimiter character (a prefixed '+' sign, or separator chars).
  43. //
  44. // Accepts:
  45. // (123) 456-7890
  46. // +38755233976
  47. // Does not accept:
  48. // 1234567890 (no delimiter chars - may just be a random number that's not a phone number)
  49. var hasDelimiters = phoneNumberText.charAt(0) === '+' || hasDelimCharsRe.test(phoneNumberText);
  50. return hasDelimiters && validPhoneNumberRe.test(phoneNumberText);
  51. }
  52. //# sourceMappingURL=phone-number-utils.js.map