php 表单验证实现代码

复制代码 代码如下:


<html>
<head>
<title>Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script language="javascript" src="https://www.jb51.net/form.js" src="https://www.jb51.net/form.js"></script>
</head>

<body>
<form action="post.php" method="get" onsubmit="return form_sub()">
<table cellpadding="0" cellspacing="0">
<tr>
<td><div>姓名:</div></td>
<td><input type="text"></td>
</tr>
<tr>
<td><div>密码:</div></td>
<td><input type="password"></td>
</tr>
<tr>
<td><div>密码确认:</div></td>
<td><input type="password"></td>
</tr>
<tr>
<td><div>性别:</div></td>
<td><select>
<option value="0" selected>男</option>
<option value="1">女</option>
</select></td>
</tr>
<tr>
<td><div>生日:</div></td>
<td><input type="text"></td>
</tr>
<tr>
<td><div>E-mail:</div></td>
<td><input type="text"></td>
</tr>
<tr>
<td><div>职业:</div></td>
<td><input type="text"></td>
</tr>
</table>
<p>
<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 cellpadding="0" cellspacing="0">
<tr>
<td><div>姓名:</div></td>
<td><?php echo $_GET['username'] ?></td>
</tr>
<tr>
<td><div>密码:</div></td>
<td><?php echo $_GET['password'] ?></td>
</tr>
<tr>
<td><div>性别:</div></td>
<td><?php if($_GET['sex']==0) echo "男"; else echo "女" ?></td>
</tr>
<tr>
<td><div>生日:</div></td>
<td><?php echo $_GET['birthday'] ?></td>
</tr>
<tr>
<td><div>E-mail:</div></td>
<td><?php echo $_GET['email'] ?></td>
</tr>
<tr>
<td><div>职业:</div></td>
<td><?php echo $_GET['job'] ?></td>
</tr>
</table>
</body>
</html>
<?php
}
?>

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/6e0757f976b5ddfe681273a4de64ef1f.html