phone.us.inc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * @file
  4. * CCK Field for North American phone numbers.
  5. */
  6. /**
  7. * Verifies that $number is a valid ten-digit North American phone number.
  8. *
  9. * @param $number
  10. * Digits only value.
  11. * @param $ext
  12. * Digits only value.
  13. * @param $error
  14. * The error message to shown to user.
  15. * Available parameters to use in the error message are
  16. * - "%countrycode": the alpha-2 CC
  17. * - "%phone_input": the original number input by user (could be invalid)
  18. * - "%max_length": allowed maximum length of the phone number
  19. * @return boolean
  20. * TRUE if it is a valid phone number for that country, FALSE otherwise.
  21. */
  22. function us_validate_number($number, $ext = '', &$error) {
  23. // Don't need to check for extension because it has been checked by generic validation as all digits, unless has special format/requirements
  24. // We don't want to worry about separators
  25. $number = cck_phone_clean_number($number);
  26. // define regular expression
  27. $regex = '/^
  28. ([1]*) # an optional 1
  29. [2-9][0-8]\d # area code (Allowed range of [2-9] for the first digit, [0-8] for the second, and [0-9] for the third digit)
  30. [2-9]\d{2} # 3-digit prefix (cannot start with 0 or 1)
  31. \d{4} # 4-digit line number
  32. $/x';
  33. $result = preg_match($regex, $number, $matches);
  34. if ($result && $matches[1] == '') {
  35. return TRUE;
  36. }
  37. elseif ($result && $matches[1] == '1') {
  38. // t() is no needed
  39. $error = 'Please enter a 10 digit North American phone number like "999 999 9999", without the country code "1" or "+1"';
  40. return FALSE;
  41. }
  42. else {
  43. // t() is no needed
  44. $error = '"%phone_input" is not a valid North American phone number, it should be a 10 digit number like "999 999 9999"';
  45. return FALSE;
  46. }
  47. }
  48. /**
  49. * Cleanup user-entered values for a phone number field for saving to DB.
  50. *
  51. * @param $number
  52. * A single phone number item.
  53. */
  54. function us_sanitize_number(&$number) {
  55. // Remove prefix '1'
  56. $number = preg_replace('/^([1]*)/', '', $number);
  57. }
  58. /**
  59. * Default formatter for international phone number.
  60. *
  61. * @param $element
  62. * $element['country_codes']: alpha-2 country code
  63. * $element['number']: phone number
  64. * @param $error
  65. * The error message to shown to user.
  66. * Available parameters to use in the error message are
  67. * - "%countrycode": the alpha-2 CC
  68. * - "%phone_input": the original number input by user (could be invalid)
  69. * - "%max_length": allowed maximum length of the phone number
  70. * @return boolean
  71. * TRUE if it is a valid phone number for that country, FALSE otherwise.
  72. */
  73. function us_formatter_default($element) {
  74. $phone = '';
  75. // Display a global phone number with country code.
  76. $cc = cck_phone_countrycodes($element['country_codes']);
  77. // Format the phone number however you like, this is the default
  78. // define regular expression
  79. $regex = '/^
  80. ([2-9][0-8]\d) # area code (Allowed range of [2-9] for the first digit, [0-8] for the second, and [0-9] for the third digit)
  81. ([2-9]\d{2}) # 3-digit prefix (cannot start with 0 or 1)
  82. (\d{4}) # 4-digit line number
  83. /x';
  84. $result = preg_match($regex, $element['number'], $matches);
  85. if ($result) {
  86. // output as +1 (AAA) BBB-CCCC
  87. $phone = $cc['code'] . ' (' . $matches[1] . ') ' . $matches[2] . '-' . $matches[3];
  88. }
  89. return $phone;
  90. }
  91. /**
  92. * Local formatter for local phone number.
  93. *
  94. * @param $element
  95. * $element['country_codes']: alpha-2 country code
  96. * $element['number']: phone number
  97. * @param $error
  98. * The error message to shown to user.
  99. * Available parameters to use in the error message are
  100. * - "%countrycode": the alpha-2 CC
  101. * - "%phone_input": the original number input by user (could be invalid)
  102. * - "%max_length": allowed maximum length of the phone number
  103. * @return boolean
  104. * TRUE if it is a valid phone number for that country, FALSE otherwise.
  105. */
  106. function us_formatter_local($element) {
  107. $phone = '';
  108. // Display a local phone number without country code.
  109. $regex = '/^
  110. ([2-9][0-8]\d) # area code (Allowed range of [2-9] for the first digit, [0-8] for the second, and [0-9] for the third digit)
  111. ([2-9]\d{2}) # 3-digit prefix (cannot start with 0 or 1)
  112. (\d{4}) # 4-digit line number
  113. /x';
  114. $result = preg_match($regex, $element['number'], $matches);
  115. if ($result) {
  116. // output as (AAA) BBB CCCC
  117. $phone = '(' . $matches[1] . ') ' . $matches[2] . '-' . $matches[3];
  118. }
  119. return $phone;
  120. }