欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php 表单验证实现代码

程序员文章站 2022-05-20 17:03:53
复制代码 代码如下: form
复制代码 代码如下:

<html>
<head>
<title>form</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<script language="javascript" src="form.js" src="form.js"></script>
</head>

<body>
<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">姓名:</div></td>
<td width="186"><input name="username" type="text" id="username"></td>
</tr>
<tr>
<td><div align="right">密码:</div></td>
<td><input name="password" type="password" id="password"></td>
</tr>
<tr>
<td><div align="right">密码确认:</div></td>
<td><input name="password2" type="password" id="password2"></td>
</tr>
<tr>
<td><div align="right">性别:</div></td>
<td><select name="sex" id="sex">
<option value="0" selected>男</option>
<option value="1">女</option>
</select></td>
</tr>
<tr>
<td><div align="right">生日:</div></td>
<td><input name="birthday" type="text" id="birthday"></td>
</tr>
<tr>
<td><div align="right">e-mail:</div></td>
<td><input name="email" type="text" id="email"></td>
</tr>
<tr>
<td><div align="right">职业:</div></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">
</p>
</form>
</body>
</html>


复制代码 代码如下:

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(str_username)
{
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(str_email)
{
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==str_p2)
return true;
else
return false;
}


复制代码 代码如下:

<?php
//本程序用于接收来自html页面的表单数据并进行相应的验证
$founderr = false; //初始化founderr变量,表示没有错误
if(!ereg("[a-za-z_]", $_get['username']))
{
echo "姓名格式不正确<br>";
$founderr = true;
}

if(!ereg("[0-9]{4}-[0-9]{2}-[0-9]{2}", $_get['birthday']))
{
echo "日期格式不正确<br>";
$founderr = true;
}

if(!ereg("^[a-za-z0-9_.]+@([a-za-z0-9_]+.)+[a-za-z]{2,3}$", $_get['email']))
{
echo "e-mail地址格式不正确<br>";
$founderr = true;
}

if($_get['password'] != $_get['password2'])
{
echo "两次密码输入不相同";
$founderr = true;
}

if(!$founderr)
{
?>
<html>
<head>
<title>form</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
</head>

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