You are here

function tfa_basic_valid_number in TFA Basic plugins 7

Validate phone number for use in TFA SMS plugin.

Parameters

string $number: Number.

Return value

array() Array of error messages if the number is not valid, empty array otherwise.

1 call to tfa_basic_valid_number()
tfa_basic_setup_form_validate in ./tfa_basic.pages.inc
Setup form validate.

File

./tfa_basic.module, line 474

Code

function tfa_basic_valid_number($number) {
  $errors = array();
  if (variable_get('tfa_basic_sms_nanp_validate', 1)) {

    // Strip leading '1' if set.
    if (strpos($number, '1') === 0) {
      $number = ltrim($number, '1');
    }

    // Validate against North American Numbering Plan (NANP) regex.
    // http://en.wikipedia.org/wiki/North_American_Numbering_Plan#Numbering_system
    $pattern = '~^\\(?([2-9][0-9]{2})\\)?[-. ]?([2-9](?!11)[0-9]{2})[-. ]?([0-9]{4})$~';
    if (!preg_match($pattern, $number)) {
      $errors = array(
        t('Number does not match expected patterns.'),
      );
    }
  }
  else {

    // For international numbers we only validate that the number contains only
    // space, hyphen, plus and digits.
    if (!preg_match('/[0-9\\-\\+ ]/', $number)) {
      $errors = array(
        t('The phone number must only contain digits, space, hyphen or plus.'),
      );
    }
  }
  $alterable = array(
    'number' => $number,
    'errors' => $errors,
  );
  drupal_alter('tfa_basic_valid_number', $alterable);
  return $alterable['errors'];
}