功能实现目标:
1、验证用户名是否为8位以上;
2、验证邮箱是否合法。
实现思路:
1、表单,post方法,请求check.php文件进行验证,注册页面为formsubmit.php;
2、首先验证用户名是否填写,如果已填写,是否大于等于8位;
3、用户验证成功后 ,验证邮箱是否填写,填写的邮箱是否有效;
4、用户名和邮箱验证都验证通过,打开success.php。
运行结果截图如下:
formsubmit.php代码如下:
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>php表单处理</title> <style> .confirm { height: 40px; line-height: 40px; font-size: 16px; font-family: 微软雅黑; background: linear-gradient(#57b4f0,#33a4ec); border: 1px solid #2899e1; color: #FFFFFF; } .button input { display: block; width: 200px; } .form { width: 50%; margin: 30px auto; } legend { font-size: 18px; color: #000077; font-weight: bolder; } </style> </head> <body> <div class="container" > <div class="row form"> <form action="check.php" method="post"> <fieldset> <legend>用户注册</legend> <p><label>用户名:<input type="text" name="name" id="name"><span style="color: red;margin-left: 5px">*</span></label></p> <p><label>邮 箱:<input type="text" name="email" id="email"><span style="color: red;margin-left: 5px">*</span></label></p> <p>性 别: <label><input type="radio" name="gender" value="male" checked>男</label> <label><input type="radio" name="gender" value="female">女</label> </p> <p> <label> 年 龄: <select name="age"> <option value="1">18岁以下</option> <option value="2">18—60岁之间</option> <option value="3">60岁以上</option> </select> </label> </p> <p> <label> 备 注: <textarea name="textarea" id="" cols="30" rows="5"></textarea> </label> </p> </fieldset> <p align="center" class="button"> <input type="submit" class="confirm" value="提 交"> </p> </form> </div> </div> </body> </html>
check.php文件代码如下:
<?php header('Content-Type: text/html; charset=utf-8'); //验证用户名 $userName = isset($_POST['name']) ? $_REQUEST['name'] : null; if (empty($userName)){ echo '<script>alert("请输入用户名");window.location.href="formsubmit.php"</script>'; }else { if (strlen($userName) < 8) { echo '<script>alert("用户名至少8位,请重新输入");window.location.href="formsubmit.php"</script>'; } else { //验证邮箱 $email = isset($_POST["email"]) ? $_REQUEST['email'] : null; $reply = ""; $pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i"; if ( preg_match( $pattern, $email ) ) { // $reply = "您输入的电子邮件地址合法<br /><br />\n"; $user_name = preg_replace( $pattern ,"$1", $email ); $domain_name = preg_replace( $pattern ,"$2", $email ); $reply .= "用户名:".$user_name."<br />\n"; $reply .= "域名:".$domain_name."<br />\n\n"; echo '<script>this.window.alert("登陆成功");window.location.href="success.php"</script>'; } else { // $reply = "您输入的电子邮件地址不合法,请重新输入"; echo '<script>this.window.alert("您输入的电子邮件地址不合法,请重新输入");window.location.href="formsubmit.php"</script>'; } } } //获取性别 $gender = $_REQUEST['gender']; //获取年龄 $age = $_REQUEST['age']; //获取备注内容 $textarea = $_REQUEST['textarea'];
success.php文件代码:
<?php header('Content-Type: text/html; charset=utf-8'); echo '欢迎进入网站管理后台~~';