加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php 表单验证实现代码

发布时间:2020-12-13 05:30:36 所属栏目:PHP教程 来源:网络整理
导读:div class="codetitle" a style="CURSOR: pointer" data="66553" class="copybut" id="copybut66553" onclick="doCopy('code66553')" 代码如下: div class="codebody" id="code66553" Form meta http-equiv="Content-Type" content="text/html; charset=gb23

<div class="codetitle"><a style="CURSOR: pointer" data="66553" class="copybut" id="copybut66553" onclick="doCopy('code66553')"> 代码如下:<div class="codebody" id="code66553">


Form
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script language="javascript" src="form.js" src="form.js">

<form action="post.php" method="get" name="form1" onsubmit="return form_sub()">
<table width="271" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="85"><div align="right">姓名:</td>
<td width="186"><input name="username" type="text" id="username"></td>
</tr>
<tr>
<td><div align="right">密码:</td>
<td><input name="password" type="password" id="password"></td>
</tr>
<tr>
<td><div align="right">密码确认:</td>
<td><input name="password2" type="password" id="password2"></td>
</tr>
<tr>
<td><div align="right">性别:</td>
<td><select name="sex" id="sex">

<option value="1">女
</td>
</tr>
<tr>
<td><div align="right">生日:</td>
<td><input name="birthday" type="text" id="birthday"></td>
</tr>
<tr>
<td><div align="right">E-mail:</td>
<td><input name="email" type="text" id="email"></td>
</tr>
<tr>
<td><div align="right">职业:</td>
<td><input name="job" type="text" id="job"></td>
</tr>
</table>
<p align="center">
<input type="submit" value="Submit">
<input type="reset" value="Reset">





<div class="codetitle"><a style="CURSOR: pointer" data="58267" class="copybut" id="copybut58267" onclick="doCopy('code58267')"> 代码如下:<div class="codebody" id="code58267">
function form_sub()
{
if(!test_username(document.form1.username.value))
{
alert("姓名格式不正确");
return false;
} if(!test_date(document.form1.birthday.value))
{
alert("日期格式不正确");
return false;
} if(!test_email(document.form1.email.value))
{
alert("E-mail地址格式不正确");
return false;
} if(!test_password(document.form1.password.value,document.form1.password2.value))
{
alert("两次密码输入不相同");
return false;
}
} function test_username(strusername)
{
var pattern = /[a-zA-Z
]/;
if(pattern.test(str_username))
return true;
else
return false;
} function test_date(str_birthday)
{
var pattern = /[0-9]{4}-[0-9]{2}-[0-9]{2}/;
if(pattern.test(str_birthday))
return true;
else
return false;
} function test_email(stremail)
{
var pattern = /^[a-zA-Z0-9
.]+@([a-zA-Z0-9_]+.)+[a-zA-Z]{2,3}$/;
if(pattern.test(str_email))
return true;
else
return false;
} function test_password(str_p1,str_p2)
{
if(str_p1==strp2)
return true;
else
return false;
}
<div class="codetitle"><a style="CURSOR: pointer" data="36241" class="copybut" id="copybut36241" onclick="doCopy('code36241')"> 代码如下:<div class="codebody" id="code36241">
<?php
//本程序用于接收来自HTML页面的表单数据并进行相应的验证
$founderr = false; //初始化founderr变量,表示没有错误
if(!ereg("[a-zA-Z
]",$_GET['username']))
{
echo "姓名格式不正确
";
$founderr = true;
} if(!ereg("[0-9]{4}-[0-9]{2}-[0-9]{2}",$GET['birthday']))
{
echo "日期格式不正确
";
$founderr = true;
} if(!ereg("^[a-zA-Z0-9
.]+@([a-zA-Z0-9_]+.)+[a-zA-Z]{2,3}$",$_GET['email']))
{
echo "E-mail地址格式不正确
";
$founderr = true;
} if($_GET['password'] != $_GET['password2'])
{
echo "两次密码输入不相同";
$founderr = true;
} if(!$founderr)
{
?>


Form
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<table width="271" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="85"><div align="right">姓名:</td>
<td width="186"><?php echo $_GET['username'] ?></td>
</tr>
<tr>
<td><div align="right">密码:</td>
<td><?php echo $_GET['password'] ?></td>
</tr>
<tr>
<td><div align="right">性别:</td>
<td><?php if($_GET['sex']==0) echo "男"; else echo "女" ?></td>
</tr>
<tr>
<td><div align="right">生日:</td>
<td><?php echo $_GET['birthday'] ?></td>
</tr>
<tr>
<td><div align="right">E-mail:</td>
<td><?php echo $_GET['email'] ?></td>
</tr>
<tr>
<td><div align="right">职业:</td>
<td><?php echo $_GET['job'] ?></td>
</tr>
</table>


<?php
}
?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读