Jump to content

PHP Алгоритм проверки номера телефона


KirKMS

Recommended Posts

Всем доброго времени суток.

Может кому пригодиться валидация номера телефона на PHP.

Идеально подойдёт для проверки при регистрации или авторизации итд.

 

function is_valid_phone($tel){
    $tel = trim((string)$tel);
    if (!$tel) return false;
    $tel = preg_replace('#[^0-9+]+#uis', '', $tel);
    if (!preg_match('#^(?:\\+?7|8|)(.*?)$#uis', $tel, $m)) return false;
    $tel = '+7' . preg_replace('#[^0-9]+#uis', '', $m[1]);
    if (!preg_match('#^\\+7[0-9]{10}$#uis', $tel, $m)) return false;
    return $tel;
}

Использование 

$MobilePhone = '89991992545';

//Проверка телефона
   if (is_valid_phone($MobilePhone)) {
      echo 'ok';
   }else {
     echo 'bad';
   }

Поддержка форматов

89

+7

79

  • Upvote 2
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...