帮助验证规则

时间:2010-03-20 21:47:37

标签: php

我试图弄清楚如何使用php验证表单的一部分。如果检查值1-5中的至少一个,则必须检查值A-E中的至少一个。如果没有检查1-5中的至少一个,则不允许使用值A-E。只要在每个部分中检查到至少一个值,就可以选择每个部分中的多个值。

我已尝试过单独的IF语句和数组但没有成功。

有没有人有任何建议或例子?我错过了一些东西,我确信这很明显,就在我的脸上。

 <div class="grid_12 alpha">
<strong>DISMISSAL</strong> - This section is to be used for errors which require dismissal by a Judge.
</div>

<div class="grid_5 alpha">
<input class="checkbox" type="checkbox" name="dis_checkbox_a" />A
&nbsp;
<input class="checkbox" type="checkbox" name="dis_checkbox_b" />B
&nbsp;
<input class="checkbox" type="checkbox" name="dis_checkbox_c" />C
&nbsp;
<input class="checkbox" type="checkbox" name="dis_checkbox_d" />D
&nbsp;
<input class="checkbox" type="checkbox" name="dis_checkbox_e" />E
</div>

<div class="grid_3">
<input class="checkbox" type="checkbox" name="dis_checkbox_mon" />Missing Officers Name
</div>

<div class="grid_4 omega">
<input class="checkbox" type="checkbox" name="dis_checkbox_civ_crim_error" />Criminal/Civil Designation in Error
</div>

<div class="grid_12 alpha">
<input class="checkbox" type="checkbox" name="dis_checkbox_improp_code_descr" />Improper Statute Number AND Description
</div>

<div class="grid_12 alpha">
<input class="checkbox" type="checkbox" name="dis_checkbox_other" />Other
<input id="input" type="text" name="dis_cit_other" size="115" maxlength="110" onkeypress="checkCapsLock( event )" />
</div>

<div class="grid_6 alpha">
Citing Officer:
<input id="input" type="text" name="dis_cit_officer" size="5" maxlength="5" onkeypress="checkCapsLock( event )" />
(Enter PR# only)
</div>

<div class="grid_6 omega">
Supervisor Notified:
<input id="input" type="text" name="dis_supvsr_notified" size="5" maxlength="5" onkeypress="checkCapsLock( event )" />
(Enter PR# only)
</div>

1 个答案:

答案 0 :(得分:1)

$number_boxes = array($_POST['box1'], $_POST['box2'], $_POST['box3'], $_POST['box4'], $_POST['box5']);

$letter_boxes = array($_POST['box_a'], $_POST['box_b'], $_POST['box_c'], $_POST['box_d'], $_POST['box_e']);

function validateCheckboxes($number_boxes, $letter_boxes){

    $numbers_checked = FALSE;
    $letters_checked = FALSE;

    foreach($number_boxes as $val){
        if($val) $numbers_checked = TRUE;
    }

    foreach($letter_boxes as $val){
        if($val) $letters_checked = TRUE;
    }

    if ($numbers_checked && !$letters_checked){
        return FALSE;
    } else if(!$numbers_checked && $letters_checked){
        return FALSE;
    } else {
        return TRUE;
    }
}