当前位置:Gxlcms > PHP教程 > php表单验证实现代码

php表单验证实现代码

时间:2021-07-01 10:21:17 帮助过:30人阅读

php 表单验证实现代码

  1. <html>
  2. <head>
  3. <title>Form</title>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  5. <script language="javascript" src="form.js" src="form.js"></script>
  6. </head>
  7. <body>
  8. <form action="post.php" method="get" name="form1" onsubmit="return form_sub()">
  9. <table width="271" border="0" align="center" cellpadding="0" cellspacing="0">
  10. <tr>
  11. <td width="85"><p align="right">姓名:</p></td>
  12. <td width="186"><input name="username" type="text" id="username"></td>
  13. </tr>
  14. <tr>
  15. <td><p align="right">密码:</p></td>
  16. <td><input name="password" type="password" id="password"></td>
  17. </tr>
  18. <tr>
  19. <td><p align="right">密码确认:</p></td>
  20. <td><input name="password2" type="password" id="password2"></td>
  21. </tr>
  22. <tr>
  23. <td><p align="right">性别:</p></td>
  24. <td><select name="sex" id="sex">
  25. <option value="0" selected>男</option>
  26. <option value="1">女</option>
  27. </select></td>
  28. </tr>
  29. <tr>
  30. <td><p align="right">生日:</p></td>
  31. <td><input name="birthday" type="text" id="birthday"></td>
  32. </tr>
  33. <tr>
  34. <td><p align="right">E-mail:</p></td>
  35. <td><input name="email" type="text" id="email"></td>
  36. </tr>
  37. <tr>
  38. <td><p align="right">职业:</p></td>
  39. <td><input name="job" type="text" id="job"></td>
  40. </tr>
  41. </table>
  42. <p align="center">
  43. <input type="submit" value="Submit">
  44. <input type="reset" value="Reset">
  45. </p>
  46. </form>
  47. </body>
  48. </html>
  1. function form_sub()
  2. {
  3. if(!test_username(document.form1.username.value))
  4. {
  5. alert("姓名格式不正确");
  6. return false;
  7. }
  8. if(!test_date(document.form1.birthday.value))
  9. {
  10. alert("日期格式不正确");
  11. return false;
  12. }
  13. if(!test_email(document.form1.email.value))
  14. {
  15. alert("E-mail地址格式不正确");
  16. return false;
  17. }
  18. if(!test_password(document.form1.password.value, document.form1.password2.value))
  19. {
  20. alert("两次密码输入不相同");
  21. return false;
  22. }
  23. }
  24. function test_username(str_username)
  25. {
  26. var pattern = /[a-zA-Z_]/;
  27. if(pattern.test(str_username))
  28. return true;
  29. else
  30. return false;
  31. }
  32. function test_date(str_birthday)
  33. {
  34. var pattern = /[0-9]{4}-[0-9]{2}-[0-9]{2}/;
  35. if(pattern.test(str_birthday))
  36. return true;
  37. else
  38. return false;
  39. }
  40. function test_email(str_email)
  41. {
  42. var pattern = /^[a-zA-Z0-9_.]+@([a-zA-Z0-9_]+.)+[a-zA-Z]{2,3}$/;
  43. if(pattern.test(str_email))
  44. return true;
  45. else
  46. return false;
  47. }
  48. function test_password(str_p1, str_p2)
  49. {
  50. if(str_p1==str_p2)
  51. return true;
  52. else
  53. return false;
  54. }
  1. <?php
  2. //本程序用于接收来自HTML页面的表单数据并进行相应的验证
  3. $founderr = false; //初始化founderr变量,表示没有错误
  4. if(!ereg("[a-zA-Z_]", $_GET['username']))
  5. {
  6. echo "姓名格式不正确<BR>";
  7. $founderr = true;
  8. }
  9. if(!ereg("[0-9]{4}-[0-9]{2}-[0-9]{2}", $_GET['birthday']))
  10. {
  11. echo "日期格式不正确<BR>";
  12. $founderr = true;
  13. }
  14. if(!ereg("^[a-zA-Z0-9_.]+@([a-zA-Z0-9_]+.)+[a-zA-Z]{2,3}$", $_GET['email']))
  15. {
  16. echo "E-mail地址格式不正确<BR>";
  17. $founderr = true;
  18. }
  19. if($_GET['password'] != $_GET['password2'])
  20. {
  21. echo "两次密码输入不相同";
  22. $founderr = true;
  23. }
  24. if(!$founderr)
  25. {
  26. ?>
  27. <html>
  28. <head>
  29. <title>Form</title>
  30. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  31. </head>
  32. <body>
  33. <table width="271" border="0" align="center" cellpadding="0" cellspacing="0">
  34. <tr>
  35. <td width="85"><p align="right">姓名:</p></td>
  36. <td width="186"><?php echo $_GET['username'] ?></td>
  37. </tr>
  38. <tr>
  39. <td><p align="right">密码:</p></td>
  40. <td><?php echo $_GET['password'] ?></td>
  41. </tr>
  42. <tr>
  43. <td><p align="right">性别:</p></td>
  44. <td><?php if($_GET['sex']==0) echo "男"; else echo "女" ?></td>
  45. </tr>
  46. <tr>
  47. <td><p align="right">生日:</p></td>
  48. <td><?php echo $_GET['birthday'] ?></td>
  49. </tr>
  50. <tr>
  51. <td><p align="right">E-mail:</p></td>
  52. <td><?php echo $_GET['email'] ?></td>
  53. </tr>
  54. <tr>
  55. <td><p align="right">职业:</p></td>
  56. <td><?php echo $_GET['job'] ?></td>
  57. </tr>
  58. </table>
  59. </body>
  60. </html>
  61. <?php
  62. }
  63. ?>

更多php 表单验证实现代码相关文章请关注PHP中文网!

人气教程排行