新建一个php文件 代码如下,将表单的action地址设为demo.php;验证方法未 post传送;在demo.php 中进行表达验证;
<!DOCTYPE html> <html> <meta charset="UTF-8"> <head><title>表单</title> <link rel="stylesheet" href="dist/css/bootstrap.css"> <script src=" https://code.jquery.com/jquery-3.2.1.min.js"></script> <script src="dist/js/bootstrap.js"></script> </head> <body> <div class="container"> <div class="row" style="margin-top:50px;"> <div class="col-md-4 col-md-push-4"> <form action="demo.php" method="post"> <fieldset> <legend> <p><label>用户名:<input type="text" name="name" placeholder="请输入用户名"></label></p> <p><label>密 码:<input type="password" name="password" placeholder="请输入密码"></label></p> <p><label>邮 箱:<input type="email" name="email" value=''></label></p> <p><label>年 龄:</label> <select name="select" > <option name='select' value=''>请选择</option> <option name='select' value='select1'>18-25</option> <option name='select' value='select2'>25-40</option> <option name='select' value='select3'>40-60</option> <option name='select' value='select4'>60以上</option> </select></p> <p> <label>性 别:</label> <input type="radio" name="gender" value='male'>男 <input type="radio" name='gender' value='woman'>女 </p> <label>备注<textarea name='textarea' rows='10' cols='30' value=''> </textarea></label> <p style="text-align:center;"><input type="submit" name="submit" value='提交'></p> </legend> </fieldset> </form> </div> </div> </div>
</body> </html>
下面是demo.php中的表单验证代码
<?php header('Content-Type:text/html; charset=utf-8'); $name=isset($_POST['name'])?$_POST['name']:null; //验证用户名不能小于两位,不能大于十位不能为空 if(empty($name)){ echo "<script>alert('你没有输入用户名');history.go(-1)</script>"; exit(); }elseif(strlen($name)<2){ echo "<script>alert('用户名不能小于两位');history.go(-1)</script>"; exit(); }elseif(strlen($name)>10){ echo "<script>alert('用户名不能大于十位');history.go(-1)</script>"; exit(); }else{ echo $name; } //验证密码不能为空,不能小于六位; $password=isset($_POST['password'])?$_POST['password']:null; if(empty($password)){ echo "<script>alert('密码不能为空');history.go(-1)</script>"; exit(); }elseif(strlen($password)<6){ echo "<script>alert('密码不能小于六位');history.go(-1)</script>"; exit(); }else{ echo '<script>alert("密码是"'.$password.');history.back()</script>'; } //验证邮件不能为空,格式必须正确; $email=isset($_POST['email'])?$_POST['email']:null; if(empty($email)){ echo "<script>alert('电子邮件不能为空');history.back()</script>"; exit(); }elseif(!preg_match("/[\w\-]+\@[\w\-]+\.[\w\-]+/",$email)){ echo '<script>alert("电子邮件不合法");history.back()</script>'; exit(); }else{ echo $email; } //验证年龄不能为空 $age=isset($_POST['select'])?$_POST['select']:null; if(empty($age)){ echo "<script>alert('请选择年龄');history.back()</script>"; exit; }else{ switch($age){ case 'select1': echo '<script>alert("18-25")</script>'; break; case 'select2': echo '<script>alert("25-40")</script>'; break; case'select3' : echo '<script>alert("40-60")</script>'; case 'select4': echo '<script>alert("60岁以上")</script>'; break; default: return $age; } echo $age; } $gender=isset($_POST['gender'])?$_POST['gender']:null; //验证性别不能为空; if(empty($gender)){ echo "<script>alert('请选择性别');history.back()</script>"; exit; }else{ switch($gender){ case 'male': echo '<script>alert("男")</script>'; break; case 'woman': echo '<script>alert("女")</script>'; break; default: return $gender; } echo $gender; } ?>