Home > Backend Development > PHP Tutorial > PHP form validation implementation code_PHP tutorial

PHP form validation implementation code_PHP tutorial

WBOY
Release: 2016-07-21 15:47:37
Original
1073 people have browsed it

复制代码 代码如下:



Form



































姓名:
密码:
密码确认:
性别:
生日:
E-mail:
职业:










复制代码 代码如下:

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;
}


复制代码 代码如下:

//This program is used to receive form data from HTML pages and perform corresponding verification
$founderr = false; //Initialize the founderr variable, indicating that there is no error
if(!ereg("[a-zA-Z_]", $_GET['username']))
{
echo "The name format is incorrect
";
$founderr = true;
}

if(!ereg("[0-9]{4}-[0-9]{2}-[0-9]{2}", $_GET[' birthday']))
{
echo "The date format is incorrect
";
$founderr = true;
}

if(!ereg("^[ a-zA-Z0-9_.]+@([a-zA-Z0-9_]+.)+[a-zA-Z]{2,3}$", $_GET['email']))
{
echo "E-mail address format is incorrect
";
$founderr = true;
}

if($_GET['password'] != $_GET['password2'])
{
echo "Two password entries are not the same";
$founderr = true;
}

if(!$founderr)
{
?>


Form














< ;/tr>












Name:
Password:
Gender:
< ;/td>
Birthday:
E -mail:
Occupation:



}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319920.htmlTechArticleCopy the code as follows: html head titleForm/title meta http-equiv="Content-Type" content="text /html; charset=gb2312" script language="javascript" src="form.js" src="form.js"/scr...
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