You are here

function sms_international_validate_number in SMS Framework 6.2

Same name and namespace in other branches
  1. 7 modules/sms_international/sms_international.module \sms_international_validate_number()

Take a number including country code and make sure it is valid.

File

modules/sms_international/sms_international.module, line 11

Code

function sms_international_validate_number($number, $options = array()) {

  // Replace all non-digit charecters with spaces
  $number = preg_replace("/[^0-9]/", '', $number);
  $number = trim($number);
  $number = ltrim($number, '0');

  // Remove leading zeros
  for ($i = 0; $i < 4; $i++) {
    $candidates[] = substr($number, 0, $i + 1);
  }
  $result = db_query("SELECT iso, country_code, min_length, max_length FROM {sms_international} WHERE country_code IN (" . db_placeholders($candidates, 'varchar') . ") AND status = 1", $candidates);
  $countries = array();
  while ($country = db_fetch_array($result)) {
    $countries[] = $country;
    $codes[$country['country_code']] = $country['iso'];
  }
  foreach ($countries as $country) {
    $trimmed_number = ltrim($number, $country['country_code']);
    if (strlen($trimmed_number) >= $country['min_length'] && strlen($trimmed_number) <= $country['max_length']) {
      return FALSE;
    }
  }
  return 'false';
}