程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> php 表單驗證實現代碼

php 表單驗證實現代碼

編輯: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"><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
}
?>
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved