在PHP中使用正则表达式验证

时间:2015-11-19 06:30:40

标签: php regex forms validation

我有这个代码,我不知道为什么它不会验证。当它发现错误时,它应该显示具有无效信息的每个无效字段,但不知何故它显示所有字段无效。这是我的代码:

<?php
$debug =1;

$fname ="";
$valid_fname = 0;
$fname_regex = '/^([A-Z][a-z]){2,15}$/';
$fname_error = '<span>First name must be 2-15 charachters long.</span>';


$lname = "";
$valid_lname = 0;
$lname_regex = '/^([A-Z][a-z]){2,15}$/';
$lname_error = '<span>Last name must be 2-15 charachters long.</span>';

$phone_number = "";
$valid_phone = 0;
$phone_regex = '/^\d{3}-\d{3}-\d{4}$/';
$phone_error = '<span>Phone number must be xxx-xxx-xxxx format.</span>';

$city = "";
$valid_city = 0;
$city_regex = '/^([A-Z][a-z]){3,20}$/';
$city_error = '<span>City name must be 3-20 charachters.</span> ';

$state = "";
$valid_state = 0;
$state_regex = '/^([A-Z][a-z]){2}$/';
$state_error = '<span>State name must be 2 charachters.</span>';

$error_text = "";
$output_form = 1;

if(isset($_POST['submit'])) {

    /*if($debug) {
        echo "<pre>";
        print_r($_POST);
        echo "</pre>";
    }*/ 
    //debug only

    $fname = trim($_POST['fname']);
    $lname = trim($_POST['lname']);
    $phone_number = trim($_POST['pnumber']);
    $city = trim($_POST['city']);
    $state = trim($_POST['state']);

    if (preg_match($fname_regex, $fname)) {
        $valid_fname =1;
    } else {
        $error_text.= "$fname_error<br>\n\r";
    }

    if (preg_match($lname_regex, $lname)) {
        $valid_lname =1;
    } else {
        $error_text.= "$lname_error<br>\n\r";
    }

    if (preg_match($phone_regex, $phone_number)) {
        $valid_phone =1;

    } else {
        $error_text.= "$phone_error<br>\n\r";
    }

    if (preg_match($city_regex, $city)) {
        $valid_city =1;
    } else {
        $error_text.= "$city_error<br>\n\r";
    }

    if (preg_match($state_regex, $state)) {
        $valid_state =1;
    } else {
        $error_text.= "$state_error<br>\n\r";
    }

if ($valid_fname && $valid_lname && $valid_phone && $valid_city && $valid_state) {
        $output_form = 0;

    } else {
        $error_text .= "<span>Please complete all the fields.</span>";
    }


}//end of if(isset($_POST['submit']))

require_once("./inc/head.inc.php");

&GT;

<body>
    <div id="wrapper">
    <?php require_once("./inc/header.inc.php"); ?>
        <div class="content">
        <?php 
            if($output_form) {
        ?>
            <h3>Please fill out following information.</h3>
            <?= $error_text ?>
            <form class="myform" action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
                <p>First Name: <input name="fname" type="text" value="<?= $fname ?>">
                <p>Last Name: <input name="lname" type="text" value="<?= $lname ?>">
                <p>Phone Number <small>(Must be in xxx-xxx-xxxx format)</small><input name="pnumber" type="text" value="<?= $phone_number ?>"></p>
                <p>City:<input name="city" type="text" value="<?= $city ?>"></p>
        <p>State:<input name="state" type="text" value="<?= $state ?>"></p>
                <div class="submit">
                    <input name="submit" type="submit" value="Send">
                </div>
            </form>
        <?php
            } else {
        ?>

        <div class="output">
        <h2>Your information is valid:</h2>
        <p>Hello! <?= $fname .' '.$lname?></p>
        <p>Phone: <?= $phone_number ?></p>
        <p>Ciy, State : <?= $city . '' . $state ?></p>
        </div>
        </div>
        <?php
        }
        ?>
    </div>

    <?php require_once("./inc/footer.inc.php"); ?>
</body>

2 个答案:

答案 0 :(得分:1)

更改支架位置: 示例:

$fname_regex = '/^([A-Z][a-z]){2,15}$/';

$fname_regex = '/^([A-Z][a-z]{2,15})$/';

答案 1 :(得分:1)

你的正则表达式不正确。它将搜索AaAsBcDe之类的字符串(一对大写和小字符,以及该对的2-15次出现)

使用以下内容:

/^[A-Za-z]{2,15}$/

说明:

  • ^:字符串的开头
  • [A-Za-z]:任何大写或小写字母
  • {2-15}:前一个字符集中出现2到15个
  • $:字符串结尾

同样,您可以修改其他人。