Php form verification implementation code

Source: Internet
Author: User

Copy codeThe Code is as follows:
<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"> name: </div> </td>
<Td width = "186"> <input name = "username" type = "text" id = "username"> </td>
</Tr>
<Tr>
<Td> <div align = "right"> password: </div> </td>
<Td> <input name = "password" type = "password" id = "password"> </td>
</Tr>
<Tr>
<Td> <div align = "right"> Password confirmation: </div> </td>
<Td> <input name = "password2" type = "password" id = "password2"> </td>
</Tr>
<Tr>
<Td> <div align = "right"> Gender: </div> </td>
<Td> <select name = "sex" id = "sex">
<Option value = "0" selected> male </option>
<Option value = "1"> female </option>
</Select> </td>
</Tr>
<Tr>
<Td> <div align = "right"> birthday: </div> </td>
<Td> <input name = "birthday" type = "text" id = "birthday"> </td>
</Tr>
<Tr>
<Td> <div align = "right"> Email: </div> </td>
<Td> <input name = "email" type = "text" id = "email"> </td>
</Tr>
<Tr>
<Td> <div align = "right"> occupation: </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>


Copy codeThe Code is as follows:
Function form_sub ()
{
If (! Test_username (document. form1.username. value ))
{
Alert ("incorrect name format ");
Return false;
}

If (! Test_date (document. form1.birthday. value ))
{
Alert ("incorrect date format ");
Return false;
}

If (! Test_email (document. form1.email. value ))
{
Alert ("Incorrect email address format ");
Return false;
}

If (! Test_password (document. form1.password. value, document. form1.password2. value ))
{
Alert ("two different passwords ");
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 codeThe Code is as follows:
<? Php
// This program is used to receive and verify form data from HTML pages.
$ Founderr = false; // initialize the founderr variable, indicating no error
If (! Ereg ("[a-zA-Z _]", $ _ GET ['username'])
{
Echo "incorrect name format <BR> ";
$ Founderr = true;
}

If (! Ereg ("[0-9] {4}-[0-9] {2}-[0-9] {2 }", $ _ GET ['birthday'])
{
Echo "incorrect date format <BR> ";
$ Founderr = true;
}

If (! Ereg ("^ [a-zA-Z0-9 _.] + @ ([a-zA-Z0-9 _] + .) + [a-zA-Z] {2, 3} $ ", $ _ GET ['email '])
{
Echo "Incorrect email address format <BR> ";
$ Founderr = true;
}

If ($ _ GET ['Password']! = $ _ GET ['password2 '])
{
Echo "the two passwords are different ";
$ 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"> name: </div> </td>
<Td width = "186"> <? Php echo $ _ GET ['username']?> </Td>
</Tr>
<Tr>
<Td> <div align = "right"> password: </div> </td>
<Td> <? Php echo $ _ GET ['Password']?> </Td>
</Tr>
<Tr>
<Td> <div align = "right"> Gender: </div> </td>
<Td> <? Php if ($ _ GET ['sex'] = 0) echo "male"; else echo "female"?> </Td>
</Tr>
<Tr>
<Td> <div align = "right"> birthday: </div> </td>
<Td> <? Php echo $ _ GET ['birthday']?> </Td>
</Tr>
<Tr>
<Td> <div align = "right"> Email: </div> </td>
<Td> <? Php echo $ _ GET ['email ']?> </Td>
</Tr>
<Tr>
<Td> <div align = "right"> occupation: </div> </td>
<Td> <? Php echo $ _ GET ['job']?> </Td>
</Tr>
</Table>
</Body>
</Html>
<? Php
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.