You are here

function sms_validate_number in SMS Framework 6

Same name and namespace in other branches
  1. 5 sms.module \sms_validate_number()
  2. 6.2 sms.module \sms_validate_number()
  3. 7 sms.module \sms_validate_number()

Validate a phone number.

Gateways and other modules can be called to validate numbers by implementing hook_sms_validate(). The active gateway is called separately to validate the number. Will stop on the first error it encounters.

For historical reasons a successful return value is NULL - any other value is expected to be an error message. We would like to change this in future.

Parameters

$number str Mobile phone number:

$options int Options to be passed to the validation functions:

Return value

NULL or an error message

3 calls to sms_validate_number()
sms_send_form_validate in ./sms.module
Send form validation.
sms_user_validate_number in modules/sms_user/sms_user.module
sms_valid_admin_test_form_submit in modules/sms_valid/sms_valid.admin.inc

File

./sms.module, line 461
The core of the SMS Framework. Provides gateway managment and API for sending and receiving SMS messages.

Code

function sms_validate_number(&$number, $options = array()) {

  // Get the function names from the modules that implement hook_sms_validate().
  // We do not use module_invoke_all() because we lose the ability to
  //   manipulate the variables passed to the function, eg: $number.
  $validation_functions = array();
  foreach (module_implements('sms_validate') as $module) {
    $validation_functions[] = $module . '_sms_validate';
  }

  // Check for zero-length value
  if (!strlen($number)) {
    return t('You must enter a phone number.');
  }

  // Remove any non-digit characters, including whitespace
  $number = preg_replace('/[^\\d]/', '', $number);

  // Pre process hook
  foreach ($validation_functions as $function) {
    $error = $function('pre process', $number, $options);
    if ($error) {
      return $error;
    }
  }

  // Process hook
  foreach ($validation_functions as $function) {
    $error = $function('process', $number, $options);
    if ($error) {
      return $error;
    }
  }

  // Allow the active gateway to provide number validation
  $gateway = sms_default_gateway();
  if (function_exists($gateway['validate number']) && ($result = $gateway['validate number']($number, $options))) {
    return $result;
  }

  // Post process hook
  foreach ($validation_functions as $function) {
    $error = $function('post process', $number, $options);
    if ($error) {
      return $error;
    }
  }
}