将字段留空时的php警告消息

时间:2013-08-22 12:04:03

标签: php textbox warnings field

我有这个代码,当人们将需求字段留空时,我想制作一个警告消息框。

代码:

if (@ is_array($cfields)) {
    foreach ($cfields as $cf) {
        if (intval($cf['required']) == 1) {
            if ($cf['type'] == "text" || $cf['type'] == "textarea" || $cf['type'] == "date") {
            ?>
            if(!vrvar.vrcf<?php echo $cf['id']; ?>.value.match(/\S/)) {
                document.getElementById('vrcf<?php echo $cf['id']; ?>').style.color='#ff0000';
                return false;
            }else {
                document.getElementById('vrcf<?php echo $cf['id']; ?>').style.color='';
            }
            <?php

            }elseif ($cf['type'] == "select") {
            ?>
            if(!vrvar.vrcf<?php echo $cf['id']; ?>.value.match(/\S/)) {
                document.getElementById('vrcf<?php echo $cf['id']; ?>').style.color='#ff0000';
                return false;
            }else {
                document.getElementById('vrcf<?php echo $cf['id']; ?>').style.color='';
            }
            <?php

            } elseif ($cf['type'] == "checkbox") {
                //checkbox
            ?>
            if(vrvar.vrcf<?php echo $cf['id']; ?>.checked) {
                document.getElementById('vrcf<?php echo $cf['id']; ?>').style.color='';
            }else {
                document.getElementById('vrcf<?php echo $cf['id']; ?>').style.color='#ff0000';
                return false;
            }
            <?php

            }
        }
    }
}
?>

现在,当字段为空时,文本仅变为红色。

有什么想法吗? :)

1 个答案:

答案 0 :(得分:0)

添加提示消息,如[参见第3行]

if(!vrvar.vrcf<?php echo $cf['id']; ?>.value.match(/\S/)) {
            document.getElementById('vrcf<?php echo $cf['id']; ?>').style.color='#ff0000';
            alert('Please dont leave the field blank');// I have added here
            return false;
            }else {
            document.getElementById('vrcf<?php echo $cf['id']; ?>').style.color='';
            }