登录  /  注册

php 表单验证实现代码

高洛峰
发布: 2017-01-11 16:27:53
原创
1621人浏览过

php 表单验证实现代码

<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"><p align="right">姓名:</p></td>
<td width="186"><input name="username" type="text" id="username"></td>
</tr>
<tr>
<td><p align="right">密码:</p></td>
<td><input name="password" type="password" id="password"></td>
</tr>
<tr>
<td><p align="right">密码确认:</p></td>
<td><input name="password2" type="password" id="password2"></td>
</tr>
<tr>
<td><p align="right">性别:</p></td>
<td><select name="sex" id="sex">
<option value="0" selected>男</option>
<option value="1">女</option>
</select></td>
</tr>
<tr>
<td><p align="right">生日:</p></td>
<td><input name="birthday" type="text" id="birthday"></td>
</tr>
<tr>
<td><p align="right">E-mail:</p></td>
<td><input name="email" type="text" id="email"></td>
</tr>
<tr>
<td><p align="right">职业:</p></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[&#39;username&#39;])) 
{ 
echo "姓名格式不正确<BR>"; 
$founderr = true; 
} 

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

if(!ereg("^[a-zA-Z0-9_.]+@([a-zA-Z0-9_]+.)+[a-zA-Z]{2,3}$", $_GET[&#39;email&#39;])) 
{ 
echo "E-mail地址格式不正确<BR>"; 
$founderr = true; 
} 

if($_GET[&#39;password&#39;] != $_GET[&#39;password2&#39;]) 
{ 
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"><p align="right">姓名:</p></td> 
<td width="186"><?php echo $_GET[&#39;username&#39;] ?></td> 
</tr> 
<tr> 
<td><p align="right">密码:</p></td> 
<td><?php echo $_GET[&#39;password&#39;] ?></td> 
</tr> 
<tr> 
<td><p align="right">性别:</p></td> 
<td><?php if($_GET[&#39;sex&#39;]==0) echo "男"; else echo "女" ?></td> 
</tr> 
<tr> 
<td><p align="right">生日:</p></td> 
<td><?php echo $_GET[&#39;birthday&#39;] ?></td> 
</tr> 
<tr> 
<td><p align="right">E-mail:</p></td> 
<td><?php echo $_GET[&#39;email&#39;] ?></td> 
</tr> 
<tr> 
<td><p align="right">职业:</p></td> 
<td><?php echo $_GET[&#39;job&#39;] ?></td> 
</tr> 
</table> 
</body> 
</html> 
<?php 
} 
?>
登录后复制

更多php 表单验证实现代码相关文章请关注PHP中文网!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号