$ name = test_input ( $ _POST [ "name" ]); IF (! the preg_match ( "/ ^ [A-zA-the Z] * $ /", $ name )) { $ nameErr = "allows only letters and spaces" ; }
In Email $ = test_input ( $ _POST [ "In Email" ]); IF (! the preg_match ( "/([\w\-]+\@[\w\-]+\.[\w\-]+)/" , $ In email )) { $ emailErr = "illegal mailbox format" ; }
Website $ = test_input ( $ _POST [ "Website" ]); IF (! the preg_match ( "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0- + # & @ 9 \ /% = ~ _ |:?!.,;] * [- A-Z0-9 + & @ # \ /% = ~ _ |] / i ", $ Website )) { $ websiteErr = "illegal address of the URL" ; }
<! DOCTYPE the HTML> <HTML> <head> <Meta charset = "UTF-. 8"> <title> novice tutorial (runoob.com) </ title> <style> .error {Color: # FF0000;} </ style > </ head> <body> <? PHP // custom variable and defaults to null value $ nameErr = $ emailErr = $ genderErr = $ websiteErr = "" ; $ name = $ In Email = $ Gender = $ Comment = $ Website = "" ; IF ( $ _SERVER [ "REQUEST_METHOD"] == "POST") { IF ( empty ( $ _POST [ "name" ])) { $ nameErr = "required the Name IS" ; } the else { $ name = test_input ( $ _POST [ "name" ]); // detect whether the name spaces contain only letters with IF (! the preg_match ( "/ ^ [A-zA-the Z] * $ /", $ name )) { $ nameErr = "allows only letters and spaces" ; } } IF ( empty ( $ _POST [ "In Email" ]) ) { $ emailErr = "Email IS required"; }the else { $ In Email = test_input ( $ _POST [ "In Email" ]); // detecting whether legitimate mail IF (! the preg_match ( "/ ([\ W \ -] + \ @ [\ W \ -] + \ [\. W \ -] +) / ", $ In email )) { $ emailErr =" illegal mailbox format " ; } } IF ( empty ( $ _POST [" Website " ])) { $ Website =" " ; } the else { $ Website test_input = ( $ _POST [ "Website" ]); // detect whether the URL address legitimate IF (! Preg_match ( "/ \ b (:( ?: HTTPS | the FTP):??.? \ / \ / | The WWW \) [- A-Z0-9 + & @ # \ /% = ~ _ | !: .,;] * [- a-Z0-9 + & @ # \ /% = ~ _ |] / I ", $ Website )) { $ websiteErr =" URL address of the illegal " ; } } IF ( empty ( $ _POST [ "Comment" ])) { $ Comment = "" ; } the else { $ Comment = test_input ( $ _POST [ "Comment" ]); } IF ( empty ( $ _POST [ "Gender" ])) { $ genderErr = "gender is required."; } else { $gender = test_input($_POST["gender"]); } } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> <h2>PHP 表单验证实例</h2> <p><span class="error">* 必需字段。</span></p> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 名字: <input type="text" name="name"> <span class="error">* <?php echo $nameErr;?></span> <br><br> E-mail: <input type="text" name="email"> <span class="error">* <?php echo $emailErr;?></span> <br><br> 网址: <input type="text" name="website"> <span class="error"><?php echo $websiteErr;?></span> <br><br> 备注: <textarea name="comment" rows="5" cols="40"></textarea> <br><br> 性别: <input type="radio" name="gender" value="female">女 <input type="radio" name="gender" value="male">男 <span class="error">* <?php echo $genderErr;?></span> <br><br> <input type="submit" name="submit" value="Submit"> </form> <?php echo "<h2>您输入的内容是:</h2>"; echo $name; echo "<br>"; echo $email; echo "<br>"; echo $website; echo "<br>"; echo $comment; echo "<br>"; echo $gender; ?> </body> </html>
<! DOCTYPE the HTML> <HTML> <head> <Meta charset = "UTF-. 8"> <title> novice tutorial (runoob.com) </ title> <style> .error {Color: # FF0000;} </ style > </ head> <body> <? PHP // custom variable and defaults to null value $ nameErr = $ emailErr = $ genderErr = $ websiteErr = "" ; $ name = $ In Email = $ Gender = $ Comment = $ Website = "" ; IF ( $ _SERVER [ "REQUEST_METHOD"] == "POST") { IF ( empty ( $ _POST [ "name" ])) { $ nameErr = "name is required" ; } the else { $ name = test_input ( $ _POST [ "name" ]); // detect name contains only letters with space IF (! the preg_match ( "/ ^ [A-zA-the Z] * $ /", $ name )) { $ nameErr = "only letters and spaces" ; } } IF ( empty ( $ _POST [ "In Email" ] )) { $ emailErr = "mailbox is required" ; } //the else { $ In Email = test_input ( $ _POST [ "In Email" ]); // detecting whether legitimate mail IF (! the preg_match ( "/ ([\ W \ -] + \ @ [\ W \ -] + \ [\. W \ -] +) / ", $ In email )) { $ emailErr =" illegal mailbox format " ; } } IF ( empty ( $ _POST [" Website " ])) { $ Website =" " ; } the else { $ Website test_input = ( $ _POST [ "Website" ]); detecting the URL is legitimate if (! Preg_match ( "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%? ~ _ = |:!.,;] * [- a-Z0-9 + & @ # \ /% = ~ _ |] / i ", $ Website )) { $ websiteErr =" URL address illegal " ; } } IF ( empty ( $ _POST [ "Comment" ])) { $ Comment = "" ; } the else { $ Comment = test_input ( $ _POST [ "Comment" ]); } IF ( empty ( $ _POST [ "Gender"])) { $ genderErr = "sex is required" ; } the else { $ Gender = test_input ( $ _POST [ "Gender" ]); } } function test_input ( $ Data ) { $ Data = TRIM ( $ Data ); $ Data = stripslashes ( Data $ ); $ Data = htmlspecialchars ( $ Data ); return $ Data ; } ?> <H2> examples of the PHP form validation </ H2> <P> <span class = "error"> * required field. </ span> </ p> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 名字: <input type="text" name="name" value="<?php echo $name;?>"> <span class="error">* <?php echo $nameErr;?></span> <br><br> E-mail: <input type="text" name="email" value="<?php echo $email;?>"> <span class="error">* <?php echo $emailErr;?></span> <br><br> 网址: <input type="text" name="website" value="<?php echo $website;?>"> <span class="error"><?php echo $websiteErr;?></span> <br><br> 备注: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea> <br><br> 性别: <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">女 <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">男 <span class="error">* <?php echo $genderErr;?></span> <br><br> <input type="submit" name="submit" value="Submit"> </form> <?php echo "<h2>您输入的内容是:</h2>"; echo $name; echo "<br>"; echo $email; echo "<br>"; echo $website; echo "<br>"; echo $comment; echo "<br>"; echo $gender; ?> </body> </html>