> 后端 > php > php 表单验证实现代码_php基础 <BR> <BR><title>Form</title> <BR><meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <BR><script language="javascript" src="form.js" src="form.js"></script> <BR> <br><br><body> <BR> <BR><table width="271" border="0" align="center" cellpadding="0" cellspacing="0"> <BR><tr> <BR><td width="85"><div align="right">姓名:
|
密码:
|
|
密码确认:
|
|
性别:
|
男 女
|
生日:
|
|
E-mail:
|
|
职业:
|
|
function form_sub() <BR>{ <BR>if(!test_username(document.form1.username.value)) <BR>{ <BR>alert("姓名格式不正确"); <BR>return false; <BR>} <br><br>if(!test_date(document.form1.birthday.value)) <BR>{ <BR>alert("日期格式不正确"); <BR>return false; <BR>} <br><br>if(!test_email(document.form1.email.value)) <BR>{ <BR>alert("E-mail地址格式不正确"); <BR>return false; <BR>} <br><br>if(!test_password(document.form1.password.value, do<strong style="color:transparent">本文来源gao@daima#com搞(%代@#码@网&</strong><strong>搞gaodaima代码</strong>cument.form1.password2.value)) <BR>{ <BR>alert("两次密码输入不相同"); <BR>return false; <BR>} <BR>} <br><br>function test_username(str_username) <BR>{ <BR>var pattern = /[a-zA-Z_]/; <BR>if(pattern.test(str_username)) <BR>return true; <BR>else <BR>return false; <BR>} <br><br>function test_date(str_birthday) <BR>{ <BR>var pattern = /[0-9]{4}-[0-9]{2}-[0-9]{2}/; <BR>if(pattern.test(str_birthday)) <BR>return true; <BR>else <BR>return false; <BR>} <br><br>function test_email(str_email) <BR>{ <BR>var pattern = /^[a-zA-Z0-9_.]+@([a-zA-Z0-9_]+.)+[a-zA-Z]{2,3}$/; <BR>if(pattern.test(str_email)) <BR>return true; <BR>else <BR>return false; <BR>} <br><br>function test_password(str_p1, str_p2) <BR>{ <BR>if(str_p1==str_p2) <BR>return true; <BR>else <BR>return false; <BR>} <BR>
<?php <BR>//本程序用于接收来自HTML页面的表单数据并进行相应的验证 <BR>$founderr = false; //初始化founderr变量,表示没有错误 <BR>if(!ereg("[a-zA-Z_]", $_GET['username'])) <BR>{ <BR>echo "姓名格式不正确<BR>"; <BR>$founderr = true; <BR>} <br><br>if(!ereg("[0-9]{4}-[0-9]{2}-[0-9]{2}", $_GET['birthday'])) <BR>{ <BR>echo "日期格式不正确<BR>"; <BR>$founderr = true; <BR>} <br><br>if(!ereg("^[a-zA-Z0-9_.]+@([a-zA-Z0-9_]+.)+[a-zA-Z]{2,3}$", $_GET['email'])) <BR>{ <BR>echo "E-mail地址格式不正确<BR>"; <BR>$founderr = true; <BR>} <br><br>if($_GET['password'] != $_GET['password2']) <BR>{ <BR>echo "两次密码输入不相同"; <BR>$founderr = true; <BR>} <br><br>if(!$founderr) <BR>{ <BR>?> <BR> <BR> <BR><title>Form</title> <BR><meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <BR> <br><br><body> <BR><table width="271" border="0" align="center" cellpadding="0" cellspacing="0"> <BR><tr> <BR><td width="85"><div align="right">姓名:
<?php echo $_GET[‘username’] ?> |
密码:
|
<?php echo $_GET[‘password’] ?> |
性别:
|
<?php if($_GET[‘sex’]==0) echo “男”; else echo “女” ?> |
生日:
|
<?php echo $_GET[‘birthday’] ?> |
E-mail:
|
<?php echo $_GET[’email’] ?> |
职业:
|
<?php echo $_GET[‘job’] ?> |
<?php
}
?>