D
D
drtvader2015-10-01 16:52:19
PHP
drtvader, 2015-10-01 16:52:19

Why doesn't phone number validation work?

Why doesn't phone verification work? Where did I go wrong? Always writes what needs to be entered in letters. And I can’t turn off the captcha check, I seem to have found it, I delete it, but it still requires it.

?<?php

/* config start */

$emailAddress = '';

/* config end */


require "phpmailer/class.phpmailer.php";

session_name("fancyform");
session_start();


foreach($_POST as $k=>$v)
{
  if(ini_get('magic_quotes_gpc'))
  $_POST[$k]=stripslashes($_POST[$k]);
  
  $_POST[$k]=htmlspecialchars(strip_tags($_POST[$k]));
}


$err = array();

if(!checkLen('name'))
  $err[]='The name field is too short or empty!';

if(!phone('phone'))
  $err[]='The phone field is too short or empty!';
else if(!phone($_POST['phone']))
  $err[]='Your phone is not valid!';
  
if(!checkLen('email'))
  $err[]='The email field is too short or empty!';
  
else if(!checkEmail($_POST['email']))
  $err[]='Your email is not valid!';

if(!checkLen('subject'))
  $err[]='You have not selected a subject!';

if(!checkLen('subject1'))
  $err[]='You have not selected a subject!';

if(!checkLen('message'))
  $err[]='The message field is too short or empty!';

if((int)$_POST['captcha'] != $_SESSION['expect'])
  $err[]='The captcha code is wrong!';


if(count($err))
{
  if($_POST['ajax'])
  {
    echo '-1';
  }

  else if($_SERVER['HTTP_REFERER'])
  {
    $_SESSION['errStr'] = implode('<br />',$err);
    $_SESSION['post']=$_POST;
    
    header('Location: '.$_SERVER['HTTP_REFERER']);
  }

  exit;
}


$msg=
'<strong>Èìÿ:</strong>	'.$_POST['name'].'<br />
<strong>e-mail:</strong>	'.$_POST['email'].'<br />
<strong>IP:</strong>	'.$_SERVER['REMOTE_ADDR'].'<br /><br />

<strong>Ñîîáùåíèå:</strong><br /><br />

'.nl2br($_POST['message']).'

';


$mail = new PHPMailer();
$mail->IsMail();

$mail->AddReplyTo($_POST['email'], $_POST['name']);
$mail->AddAddress($emailAddress);
$mail->SetFrom($_POST['email'], $_POST['name']);
$mail->Subject = "Tåìà ïèñüìà: ".$_POST['subject'];

$mail->MsgHTML($msg);

$mail->Send();


unset($_SESSION['post']);

if($_POST['ajax'])
{
  echo '1';
}
else
{
  $_SESSION['sent']=1;
  
  if($_SERVER['HTTP_REFERER'])
    header('Location: '.$_SERVER['HTTP_REFERER']);
  
  exit;
}

function checkLen($str,$len=2)
{
  return isset($_POST[$str]) && mb_strlen(strip_tags($_POST[$str]),"utf-8") > $len;
}

function checkEmail($str)
{
  return preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $str);
}

function phone($phone) 
    { 
    $pattern = "/^((8)|([\+]?(7)))+(?(?=([ -]))[ -]?\(?\d{3}\)?)+(?(?=([ -]))[ -]?\d{3})+(?(?=([ -]))[ -]?\d{4})$/"; 
    if(preg_match($pattern, $phone)) 
     { 
        return true;} 
       return false;} 

?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2015-10-01
@dimonchik2013

hellish regexp)))
the phone is not a telex, there are no letters in it
1) clear letters (NOT digital characters)
2) verify

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question