0

我有一个收集信息的表格,其中一个是电话号码。电话号码数据来自三个字段,一个是区号,一个是前 3 位数字,一个是后四位数字,因此号码的格式为:xxx-xxx-xxxx(美国基本格式)。

这三个字段不是必需的,但如果有人决定填写这三个字段的任意组合,我想做一些基本的错误检查:

(假设他们只给我区号 - 这意味着他们想给我他们的号码,所以从本质上讲,它成为必需的,所以代码应该检查以查看 1)所有三个数据集都已发送,以及 2)这三个都只是数字)

这是我认为可行的方法,但它没有:

if((isset($_POST['numArea'], $_POST['numFirst'], $_POST['numSecond']) && (!ctype_digit(trim($_POST['numArea'])) || !ctype_digit(trim($_POST['numFirst'])) || !ctype_digit(trim($_POST['numSecond'])) || strlen(trim($_POST['numArea'])) !== 3 || strlen(trim($_POST['numFirst'])) !== 3 || strlen(trim($_POST['numSecond'])) !== 4))
        || (isset($_POST['numArea']) XOR isset($_POST['numFirst']) XOR isset($_POST['numArea']))){
                $errors[] = 'Please give us a valid Phone Number, or remove any numbers if you do not wish to use your phone number.';
        }else{
            $_POST['PhoneNumber'] = '+01'.$_POST['numArea'].'-'.$_POST['numFirst'].'-'.$_POST['numSecond']; }

有什么建议么?

4

5 回答 5

4

您的代码不起作用的原因不是因为您的布尔逻辑,而是因为您使用了isset(). 在 a 的情况下<input type="text">$_POST['fieldName']无论值是否为空,都将始终设置。

改为用于$_POST['fieldName'] != ''确定用户是否输入了值。不要使用empty(),因为这会将任何虚假值返回为空(、、、0000... false)。


就个人而言,我宁愿用一个<input type="type">作为电话号码。这比让用户切换框更不烦人,也使验证更简单。

这个例子实际上验证了数字是否遵循NANP规则。我发现这么多应用程序/网站监督这个验证步骤是绝对荒谬的。

// Did the user post a number?
if($_POST['phone'] != '') {

  // Get only the numbers, we don't care how the user formatted their number
  $_POST['phone'] = preg_replace('/[^0-9]/', '', $_POST['phone']);

  // Is it a valid NANP phone number?
  if(preg_match('/^1?[2-9][0-8][0-9][2-9][0-9]{6}$/i', $_POST['phone']) === 1) {
    echo "Valid NANP phone number";

    // Trim the leading one
    $_POST['phone'] = ltrim($_POST['phone'], '1');

    // Format as wanted
    $_POST['PhoneNumber'] = '+01'.substr($_POST['phone'],0,3).'-'.substr($_POST['phone'],3,3).'-'.substr($_POST['phone'],6,4);
  } else {
    echo "Invalid phone number";
  }
} else {
  echo "User didn't provide phone number";
}
于 2009-08-05T06:29:42.063 回答
1

首先,如果这些字段是输入,那么isset()将始终返回 true。您可能要检查的是它们是否不为空。所以你应该empty()为此使用函数。

我会将您的表单值替换为$a,$b$c使其变得简单。

$a = $_POST['numArea'];
$b = $_POST['numFirst'];
$c = $_POST['numSecond'];

if (!empty($a) || !empty($b) || !empty($b)) {
    // we know now that at least field was filled in, lets check their values
    $regex = '/^\d+$/';
    if (!preg_match($regex, $a) || !preg_match($regex, $b) || !preg_match($regex, $c)) {
        echo "Phone number invalid";
    }
}

这只是一个例子。您可以将其缩短为仅一个if语句,但我没有这样做以使其更具可读性。

于 2009-08-05T06:29:23.267 回答
0

只需检查是否未设置其中一个字段;

if (!isset($_REQUEST['numFirst']) || !isset($_REQUEST['numSecond']) || !isset($_REQUEST['numArea'])) {
    if (!isset($_REQUEST['numFirst'])) {
         print 'Please fill out the FIrst area';
    }
    if (!isset($_REQUEST['numSecond'])) {
         print 'Please fill out the Second area';
    }
    if (!isset($_REQUEST['numArea'])) {
         print 'Please fill out the Area code';
    }
}

那是你想做的事情吗?

于 2009-08-05T06:27:16.733 回答
0

这不是您的问题的解决方案,但它会以其他方式解决它,试试imask

它实际上是一个 JS 脚本。

于 2009-08-05T06:39:38.563 回答
0

首先,如果有人能够维护您的代码,您将不得不将其分解为方法调用。我可能会这样写:

public function phoneNumberWasProvided () {
   return !(empty($_POST['numArea']) && 
            empty($_POST['numFirst']) && 
            empty($_POST['numSecond']));

}

public function phoneNumberIsValid () {
   $this->_phoneErrors = array();
   // The following three if statements can also be
   // extracted into their own methods
   if(!preg_match("/^\d{3}/$", $_POST['numArea']) {
      $this->_phoneErrors['numArea'] = 'The area code you provided is invalid';
   }
   if(!preg_match("/^\d{3}/$", $_POST['numFirst']) {
      $this->_phoneErrors['numFirst'] = 'The first part of the provided phone 
                                         number is invalid';
   }
   if(!preg_match("/^\d{4}/$",$_POST['numSecond']) {
      $this->_phoneErrors['numArea'] = 'The first part of the provided phone 
                                        number is invalid';
   }

   return empty($this->_phoneErrors);
}

现在您可以在主逻辑中轻松使用这些方法,使其更具可读性:

if($this->phoneNumberWasProvided()) {
    if(!$this->phoneNumberIsValid()) {
        $errors = $this->getPhoneNumberErrors();
        // Print errors / do whatever is necessary
    } else {
       $phoneNumber = 
         "{$_POST['numArea']}-{$_POST['numFirst']}-{$_POST['numSecond']}";
    }
}
于 2009-08-05T06:48:12.213 回答