-----------------------------------------------------------------------------
本节展示如何验证名字、电邮和 URL。
-----------------------------------------------------------------------------
PHP - 验证名字
以下代码展示的简单方法检查 name 字段是否包含字母和空格。如果 name 字段无效,则存储一条错误消息:
1 $name = test_input($_POST["name"]); 2 if (!preg_match("/^[a-zA-Z ]*$/",$name)) { 3 $nameErr = "只允许字母和空格!"; 4 }
注释:preg_match() 函数检索字符串的模式,如果模式存在则返回 true,否则返回 false。
---------------------------------------------------------------------------------------------------------------
PHP - 验证 E-mail
以下代码展示的简单方法检查 e-mail 地址语法是否有效。如果无效则存储一条错误消息:
1 $email = test_input($_POST["email"]); 2 if (!preg_match("/([w-]+@[w-]+.[w-]+)/",$email)) { 3 $emailErr = "无效的 email 格式!"; 4 }
---------------------------------------------------------------------------------------------------------------
PHP - 验证 URL
以下代码展示的方法检查 URL 地址语法是否有效(这条正则表达式同时允许 URL 中的斜杠)。如果 URL 地址语法无效,则存储一条错误消息:
1 $website = test_input($_POST["website"]); 2 if (!preg_match("/(?:(?:https?|ftp)://|www.)[-a-z0-9+&@#/%?=~_|!:,.;]*[-a-z0-9+&@#/% 3 =~_|]/i",$website)) { 4 $websiteErr = "无效的 URL"; 5 }
-------------------------------------------------------------------------------------------------------------
PHP - 验证 Name、E-mail、以及 URL
现在,脚本是这样的:
实例
1 <?php 2 // 定义变量并设置为空值 3 $nameErr = $emailErr = $genderErr = $websiteErr = ""; 4 $name = $email = $gender = $comment = $website = ""; 5 6 if ($_SERVER["REQUEST_METHOD"] == "POST") { 7 if (empty($_POST["name"])) { 8 $nameErr = "Name is required"; 9 } else { 10 $name = test_input($_POST["name"]); 11 // 检查名字是否包含字母和空格 12 if (!preg_match("/^[a-zA-Z ]*$/",$name)) { 13 $nameErr = "Only letters and white space allowed"; 14 } 15 } 16 17 if (empty($_POST["email"])) { 18 $emailErr = "Email is required"; 19 } else { 20 $email = test_input($_POST["email"]); 21 // 检查电邮地址语法是否有效 22 if (!preg_match("/([w-]+@[w-]+.[w-]+)/",$email)) { 23 $emailErr = "Invalid email format"; 24 } 25 } 26 27 if (empty($_POST["website"])) { 28 $website = ""; 29 } else { 30 $website = test_input($_POST["website"]); 31 // 检查 URL 地址语言是否有效(此正则表达式同样允许 URL 中的下划线) 32 if (!preg_match("/(?:(?:https?|ftp)://|www.)[-a-z0-9+&@#/%?=~_|!:,.;]*[-a-z0-9+&@#/% 33 =~_|]/i",$website)) { 34 $websiteErr = "Invalid URL"; 35 } 36 } 37 38 if (empty($_POST["comment"])) { 39 $comment = ""; 40 } else { 41 $comment = test_input($_POST["comment"]); 42 } 43 44 if (empty($_POST["gender"])) { 45 $genderErr = "Gender is required"; 46 } else { 47 $gender = test_input($_POST["gender"]); 48 } 49 } 50 ?>
源代码:
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <style> 5 .error {color: #FF0000;} 6 </style> 7 </head> 8 <body> 9 10 <?php 11 // 定义变量并设置为空值 12 $nameErr = $emailErr = $genderErr = $websiteErr = ""; 13 $name = $email = $gender = $comment = $website = ""; 14 15 if ($_SERVER["REQUEST_METHOD"] == "POST") { 16 if (empty($_POST["name"])) { 17 $nameErr = "姓名是必填的"; 18 } else { 19 $name = test_input($_POST["name"]); 20 // 检查姓名是否包含字母和空白字符 21 if (!preg_match("/^[a-zA-Z ]*$/",$name)) { 22 $nameErr = "只允许字母和空格"; 23 } 24 } 25 26 if (empty($_POST["email"])) { 27 $emailErr = "电邮是必填的"; 28 } else { 29 $email = test_input($_POST["email"]); 30 // 检查电子邮件地址语法是否有效 31 if (!preg_match("/([w-]+@[w-]+.[w-]+)/",$email)) { 32 $emailErr = "无效的 email 格式"; 33 } 34 } 35 36 if (empty($_POST["website"])) { 37 $website = ""; 38 } else { 39 $website = test_input($_POST["website"]); 40 // 检查 URL 地址语法是否有效(正则表达式也允许 URL 中的斜杠) 41 if (!preg_match("/(?:(?:https?|ftp)://|www.)[-a-z0-9+&@#/%?=~_|!:,.;]*[-a-z0-9+&@#/%=~_|]/i",$website)) { 42 $websiteErr = "无效的 URL"; 43 } 44 } 45 46 if (empty($_POST["comment"])) { 47 $comment = ""; 48 } else { 49 $comment = test_input($_POST["comment"]); 50 } 51 52 if (empty($_POST["gender"])) { 53 $genderErr = "性别是必选的"; 54 } else { 55 $gender = test_input($_POST["gender"]); 56 } 57 } 58 59 function test_input($data) { 60 $data = trim($data); 61 $data = stripslashes($data); 62 $data = htmlspecialchars($data); 63 return $data; 64 } 65 ?> 66 67 <h2>PHP 验证实例</h2> 68 <p><span class="error">* 必需的字段</span></p> 69 <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 70 姓名:<input type="text" name="name"> 71 <span class="error">* <?php echo $nameErr;?></span> 72 <br><br> 73 电邮:<input type="text" name="email"> 74 <span class="error">* <?php echo $emailErr;?></span> 75 <br><br> 76 网址:<input type="text" name="website"> 77 <span class="error"><?php echo $websiteErr;?></span> 78 <br><br> 79 评论:<textarea name="comment" rows="5" cols="40"></textarea> 80 <br><br> 81 性别: 82 <input type="radio" name="gender" value="female">女性 83 <input type="radio" name="gender" value="male">男性 84 <span class="error">* <?php echo $genderErr;?></span> 85 <br><br> 86 <input type="submit" name="submit" value="提交"> 87 </form> 88 89 <?php 90 echo "<h2>您的输入:</h2>"; 91 echo $name; 92 echo "<br>"; 93 echo $email; 94 echo "<br>"; 95 echo $website; 96 echo "<br>"; 97 echo $comment; 98 echo "<br>"; 99 echo $gender; 100 ?> 101 102 </body> 103 </html>
运行结果:
接下来向您讲解如何防止表单在用户提交表单后清空所有输入字段。