Heim >Backend-Entwicklung >PHP-Tutorial > php学习之路5(表单验证)
php学习之路五(表单验证)
<html> <head> <title>PHP处理表单</title> </head> <body> <h2>请填写个人信息</h2> <form action="ChuLi1.php" method="post"> <fieldset> 姓名:<input type="text" name="name" required="required"><br> 年龄:<select name="age" > <?php for ($i=12;$i<30;$i++){?> <option value="<?php echo $i;?>"><?php echo $i;?></option> <?php }?> </select> <br> 密码:<input type="password" placeholder="请填写密码" name="password"><br> 邮箱:<input type="email" name="email"><br> <label for="mysubmit">提交</label> <input type="submit" id="mysubmit" value="提交"> <input type="reset" value="重置"><br> </fieldset> </form> </body> </html>
验证页面:
<?php $name=$_POST["name"]; $age=$_POST["age"]; $email=$_POST["email"]; $password=$_POST["password"]; //姓名验证 if (empty($name)){ echo "姓名不得为空<br>"; }else if (strlen($name)<3||strlen($name)>10){ echo "姓名长度必须在3到10之间!<br>"; }else { echo "姓名: ".$name."<br>"; } //密码验证 if (empty($password)){ echo "密码不得为空<br>"; }else if (strlen($password)<3||strlen($password)>10){ echo "密码长度必须在3到10之间!<br>"; }else if(!ereg("[0-9a-zA-Z]+",$password)){ echo "密码必须为数字和字符组合!<br>"; }else { echo "密码: ".$password."<br>"; } //邮件地址验证 if (empty($email)){ echo "邮件地址不得为空!!!"; }else if (!ereg("^[a-zA-Z0-9_.]+@([a-zA-Z0-9_]+.)+[a-zA-Z]{2,3}$", $email)){ echo "邮件地址格式不对!!!"; }else { echo "邮件地址: ".$email."<br>"; } ?> <h3><a href="FormFirst.php">返回</a></h3>