Home > Backend Development > PHP Tutorial > php form validation implementation code

php form validation implementation code

高洛峰
Release: 2023-03-04 18:26:02
Original
1940 people have browsed it

php form verification implementation code

<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>
Copy after login
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; 
}
Copy after login
<?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 
} 
?>
Copy after login

For more articles related to php form verification implementation code, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template