`

验证手机号码

 
阅读更多
function _user_information_validate(&$form, &$form_state) {
  if (empty($form_state['values']['field_phone']['und'][0]['value'])) {
      form_set_error('account][field_phone', '您必须输入手机号码!');
  }
  $mobile = isset($form_state['values']['field_phone']['und'][0]['value']) ? $form_state['values']['field_phone']['und'][0]['value'] : '';
  if(_user_is_mobile_exist($mobile)){
    form_set_error('field_phone', '该手机号已被使用');
  }
  if((!preg_match("/^[\d]{11}+$/", $mobile) || drupal_substr($mobile, 0, 1) != 1) && !empty($mobile)){
  form_set_error('field_phone','请输入有效的手机号码');

}

/**
 * 验证手机号码是否已经存在
 *
 * TRUE手机号已被使用,FALSE未被使用
 */
function _user_is_mobile_exist($mobile){
  $query = new \EntityFieldQuery();
      $count = $query
      ->entityCondition('entity_type', 'user')
      ->entityCondition('bundle', 'user')
      ->fieldCondition('field_phone', 'value', $mobile, '=')
      ->count()
      ->execute();
   if($count > 0){
     return TRUE;
   }
   else {
      return FALSE;
   }
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics