PHP表单验证:正则表达式问题

时间:2012-06-20 23:29:56

标签: php validation

我有简单的PHP验证表单,正在中途工作。如果将该字段留空并单击“提交”,则会显示正确的错误消息。问题在于正则表达式。例如,如果货币字段无效,则它不会显示正确的错误消息。在点击提交时,它会重新加载页面并删除文本框中的所有值。为什么它不会显示正确的错误信息?

HTML表单

<form action="" method="post" id="form">

                <label for="tile">Title: <em>*</em></label>
                <input type="text" name="title" id="title" value="<?php echo $form['title']; ?>"> <?php echo $error['title'] ?>


                <label for="currency">Currency: <em>*</em></label>
                <input type="text" name="currency" id="currency" value="<?php echo $form['currency']; ?>"> <?php echo $error['currency'] ?>

                <input type="submit" name="submit" id="submit">

        </form>

PHP验证器

  //variables
    $error_open = "<label class='error'>";
    $error_close = "</label>";
    $valid_form = TRUE;
    $redirect = "success.php";

    $form_elements = array('title', 'currency');
    $required = array('title', 'currency');

    foreach ($required as $require)
    {
        $error[$require] = '';
    }

    if (isset($_POST['submit']))
    {
        //process form

        //get form data
        foreach ($form_elements as $element)
        {
            $form[$element] = htmlspecialchars($_POST[$element]);
        }

        //check form elements
            //check required fields
            if ($form['title'] == '')
            {
                $error['title'] =  $error_open . "* This field is required" . $error_close;
                $valid_form = FALSE;
            }
            if ($form['currency'] == '')
            {
                $error['currency'] = $error_open . "* This field is required" . $error_close;
                $valid_form = FALSE;
            }


            //check formatting
            if ($error['title'] == '' && !preg_match('/^([A-Za-z0-9_-]+)/', $form['title']))
            {

                $error['title'] = $error_open . "* Enter a valid descriptive title" . $error_close;
                $valid_form = FALSE;

            }

            if ($error['currency'] == '' && !preg_match('/^\s*[+-]?(\d*\.\d\d)\s*$/', $form['currency']))
            {

                $error['currency'] = $error_open . " Enter a valid decimal number <br> * Do not include Dollar($) sign <br> * Example: (1.00)" . $error_close;
                $valid_form = FALSE;

            }



        //check for bad data
            if (contains_bad_str($form['title']) ||
            contains_bad_str($form['currency']))
            {
                $valid_form = FALSE;

            }

            if (contains_bad_str($form['title']) ||
            contains_bad_str($form['currency']))
            {
                $valid_form = FALSE;

            }

        //check if form is valid
        if ($valid_form)
        {

            //redirect
            header("Location: " . $redirect);

        }
        else
        {
            include('form.php');

        }

    }

2 个答案:

答案 0 :(得分:1)

这对我有用。

function contains_bad_str($s) {
    $r = preg_match ( "/cheese|rice|beans/i" , $s);
    return $r;
}

// variables
$error_open = "<label class='error'>";
$error_close = "</label>";
$valid_form = TRUE;
$redirect = "success.php";

$form_elements = array('title', 'currency');
$metadata = array('title'=> array('/^([A-Za-z0-9_-]+)/',
                                  "* Enter a valid descriptive title"),
                  'currency' => array('/^\s*[+-]?(\d*\.\d\d)\s*$/',
                                      "* Enter a valid decimal number <br/>" .
                                      "* Do not include Dollar($) sign <br/>" .
                                      "* Example: (1.00)" ));

foreach ($form_elements as $element) {
    $error[$element] = '';
}

if (isset($_POST['submit'])) {
    // get form data
    foreach ($form_elements as $element) {
        $form[$element] = htmlspecialchars($_POST[$element]);
        // check presence
        if ($form[$element] == '') {
            $error[$element] =  $error_open . "* required" . $error_close;
            $valid_form = FALSE;
        }
        // check formatting
        elseif (!preg_match($metadata[$element][0], $form[$element])) {
            $error[$element] = $error_open . $metadata[$element][1] . $error_close;
            $valid_form = FALSE;
        }
        // sanitize
        elseif (contains_bad_str($form[$element])) {
            $error[$element] =  $error_open . "* bad string" . $error_close;
            $valid_form = FALSE;
        }
    }

    if ($valid_form) {
        header("Location: " . $redirect);
    }
    else {
        include('form.php');
    }
}
else {
    // blank form
    $form = array('title'=>'', 'currency'=>'');
    $error = array('title'=>'', 'currency'=>'');
    include('form.php');
}

答案 1 :(得分:0)

你看过PHP's input filters了吗?特别是,请查看货币的FILTER_VALIDATE_FLOAT