首页 > Web开发 > 详细

PHP学习记录之表单的验证

时间:2016-07-30 00:28:24      阅读:133      评论:0      收藏:0      [点我收藏+]

1.表单的验证的小案例

HTML表单

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>demo1.html</title>
</head>

<body>
<form method="post" action="demo2.php">
用户名:<input type="text" name="username" /><br/>
密&nbsp码: <input type="password" name="password" /><br/>
邮 箱:<input type="email" name="email" /><br/>
验证码:<input type="text" size="4" name="code"/><br/>
<input type="submit" value="提交" name="send" />
</form>
</body>
</html>
</body>
</html>

  

demo2.php需要接收信息的页面

<?php
if(!isset($_POST[‘send‘]))
{
header(‘Location:demo3‘);
exit;
}
$username=htmlspecialchars(trim($_POST[‘username‘]));
$password=$_POST[‘password‘];
$email=htmlspecialchars(trim($_POST[‘email‘]));
$code=$_POST[‘code‘];
if(strlen($username)<2||strlen($username)>20)
{
echo"<script>alert(‘您输入的用户名不能小于2个字符或者大于20个字符‘);history.back()</script>";
}
if(strlen($password)<6||strlen($password)>10)
{
echo "<script>alert(‘您的密码不能小于6位或者大于10位‘);histroy.back()</script>";
}
$mode=‘/(^[\w\.]{1,20})+@+([\w]{2,})+[.]+([a-z]{2,5})/‘;//正则表达式验证邮箱
if(!preg_match($mode,$email))
{
echo "<script>alert(‘你输入的邮箱格式错误,请重新输入‘);history.back()</script>";
}
if(strlen($code)!=4||!is_numeric($code))
{
echo"<script>alert(‘你输入的验证码有误,请重新输入‘);history.back()</script>";
}
echo "您的用户名是:".$username.‘<br />‘.‘邮箱是:‘.$email;
?>

  

  

 

PHP学习记录之表单的验证

原文:http://www.cnblogs.com/niangaolizai/p/5719925.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!