2015-11-20 42 views
0

我想验证3个相关的字段。这3个字段的格式已经有几十个字段,其中许多字段正在验证中。PHP验证 - 检查是否有1个字段填充

对于所讨论的3个相关领域,我需要编写,检查是否任何3的1被填充的代码,然后将引发一个错误,如果任何其他的2都没有。

  • 所以如果A被填充而B和C不是=错误。
  • 如果B填充并且A和C不是=错误。
  • 如果C被填充并且A和B不是=错误。

    再等两个被填充;第三,没有。

喜欢:如果A和B填充和C不是=错误。

这里是我起步的地方,但不知道这是正确的验证:

if(!empty($post['email_notify_subject']) 

    && empty($post['email_notify_date']) 
    && empty($post['email_notify_emails'])) 

{ 

$errors['email_notify_date'] = 'Enter Notify Date'; 
$errors['email_notify_emails'] = 'Enter Notify Emails'; 

} 

else if (empty($post['email_notify_subject']) 

    && !empty($post['email_notify_date']) 
    && empty($post['email_notify_emails'])) 

{ 

$errors['email_notify_subject'] = 'Enter Notify Subject'; 
$errors['email_notify_emails'] = 'Enter Notify Emails'; 

} 

else if (empty($post['email_notify_subject']) 

    && empty($post['email_notify_date']) 
    && !empty($post['email_notify_emails'])) 

{ 

$errors['email_notify_subject'] = 'Enter Notify Subject'; 
$errors['email_notify_date'] = 'Enter Notify Date'; 

} 
+0

我们需要更多的代码比一行... – Machavity

+0

后这些字段的HTML代码,以及你的

一行。 –

+0

有什么好运气? –

回答

0

这是怎么回事?

if (
    (
     ! empty($post['email_notify_subject']) && 
     empty($post['email_notify_date']) && 
     empty($post['email_notify_emails']) 
    ) || 
    (
     empty($post['email_notify_subject']) && 
     ! empty($post['email_notify_date']) && 
     empty($post['email_notify_emails']) 
    ) || 
    (
     empty($post['email_notify_subject']) && 
     empty($post['email_notify_date']) && 
     ! empty($post['email_notify_emails']) 
    ) 
) { 
    // only 1 field filled out 
} 

或者还有一种方法:

$num_filled_out = 0; 
if (! empty($post['email_notify_subject'])) $num_filled_out++; 
if (! empty($post['email_notify_date'])) $num_filled_out++; 
if (! empty($post['email_notify_emails']) $num_filled_out++; 
if ($num_filled_out <= 1) { 
    // 1 or 0 filled out 
} 
+0

谢谢你,我使用了++版本,它工作 – user3784436

0

没有测试,但不妨一试:

<?php 
    if(empty(array(
    $_post['email_notify_subject'], 
    $_post['email_notify_date'], 
    $_post['email_notify_emails']))){ 
     echo "All Three filed required"; //or other instructions 
} 
    else{ 
    //Proceed with the form 
} 
0

这将是一个更好的办法,因为你可以有你的领域你想要检查一个数组中的所有相应的错误消息。而且它更具可扩展性。

请注意,您必须测试您的表单method属性是否设置为post,并且您已向输入字段提供了正确的名称。

$data_to_check = array(
    'email_notify_subject'=>array(
     'error'=>'Enter Notify Subject' 
    ), 
    'email_notify_date'=>array(
     'error'=>'Enter Notify Date' 
    ), 
    'email_notify_emails'=>array(
     'error'=>'Enter Notify Emails' 
    ), 
); 

$errors = array(); 

foreach($data_to_check as $key => $value) { 
    if (empty($post[$key])) { 
     $errors[] = $value['error']; 
    } 
} 

if (!empty($errors)) { 
    foreach($errors as $key => $value) { 
     echo '<p class="error">'.$value.'</p>'; 
    } 
} 
0

如果你只关心在年底错误,请参阅以下内容作为参考:

$post['email_notify_subject'] = array(); // same (== not ===) as null 
$post['email_notify_date']  = null;  // same (== not ===) as empty array 
$post['email_notify_emails'] = 'test string'; 

$errors['email_notify_subject'] = 'Enter Notify Subject'; 
$errors['email_notify_date']  = 'Enter Notify Date'; 
$errors['email_notify_emails'] = 'Enter Notify Emails'; 

foreach ($post as $key => $data) { 

    if (!empty($data)) { 

     unset($errors[$key]); // if $post data not empty, unset error msg 
    } 
} 

var_dump($errors); // test dump $errors 
相关问题