php表单验证

<html>
<head>
   <meta charset="utf-8">
   <title></title>
   <style>
      .error {color: #FF0000;}
   </style>
</head>
<body>

    <?php
        // 定义变量并默认设置为空值
        $fnameErr = $genderErr = $ageErr = $phoneErr = $usernErr = $passErr = $emailErr = "";
        $fname = $gender = $age = $phone = $usern = $pass = $email = "";

        if ($_SERVER["REQUEST_METHOD"] == "POST")
        {
            if (empty($_POST["fname"]))
            {
                $fnameErr = "名字是必填的";
            }
            else
            {
                $fname = test_input($_POST["fname"]);
                // 检测名字是否只包含字母跟空格
                if (!preg_match("/^[a-zA-Z ]*$/",$fname))
                {
                    $fnameErr = "只允许字母和空格";
                }
            }

            if (empty($_POST["gender"]))
            {
                $genderErr = "性别是必填的";
            }
            else
            {
                $gender = test_input($_POST["gender"]);
            }

            if (empty($_POST["phone"]))
            {
                $phoneErr = "手机是必填的";
            }
            else
            {
                $phone = test_input($_POST["phone"]);
            }

            if (empty($_POST["username"]))
            {
                $usernErr = "用户名是必填的";
            }
            else
            {
                $usern = test_input($_POST["username"]);
            }

            if (empty($_POST["password"]))
            {
                $passErr = "密码是必填的";
            }
            else
            {
                $pass = test_input($_POST["password"]);
            }

            if (empty($_POST["email"]))
            {
              $emailErr = "邮箱是必填的";
            }
            else
            {
                $email = test_input($_POST["email"]);
                // 检测邮箱是否合法
                if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
                {
                    $emailErr = "非法邮箱格式";
                }
            }
        }

        function test_input($data)
        {
            $data = trim($data);
            $data = stripslashes($data);
            $data = htmlspecialchars($data);
            return $data;
        }
    ?>

    <p><span class="error">*为必填字段</span></p>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
        姓 名: <input type="text" name="fname" value="<?php echo $fname;?>">
        <span class="error">* <?php echo $fnameErr;?></span>
        <br><br>
        性 别:
        <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?>  value="female"><input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?>  value="male"><span class="error">* <?php echo $genderErr;?></span>
        <br><br>
        年 龄: <input type="text" name="age" value="<?php echo $age;?>">
        <br><br>
        手 机: <input type="text" name="phone" value="<?php echo $phone;?>">
        <span class="error">* <?php echo $phoneErr;?></span>
        <br><br>
        用户名: <input type="text" name="username" value="<?php echo $usern;?>">
        <span class="error">* <?php echo $usernErr;?></span>
        <br><br>
        密 码: <input type="text" name="password" value="<?php echo $pass;?>">
        <span class="error">* <?php echo $passErr;?></span>
        <br><br>
        邮 箱: <input type="text" name="email" value="<?php echo $email;?>">
        <span class="error">* <?php echo $emailErr;?></span>
        <br><br>
        <br><br>
        <input type="submit" name="submit" value="注册">
    </form>

</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值