首頁 > 後端開發 > php教程 > PHP - 驗證 Name, E-mail, 和 URL

PHP - 驗證 Name, E-mail, 和 URL

WBOY
發布: 2016-07-30 13:30:49
原創
1217 人瀏覽過

以下是原始碼:

<code><span><!DOCTYPE HTML></span><span><<span>html</span>></span><span><<span>head</span>></span><span><<span>style</span>></span><span><span>.error</span><span>{<span><span>color</span>:<span><span>#FF0000</span></span></span>;<span>}</span></span></span><span></<span>style</span>></span><span></<span>head</span>></span><span><<span>body</span>></span><span><span><?php</span><span>// define variables and set to empty values</span><span>$nameErr</span> = <span>$emailErr</span> = <span>$genderErr</span> = <span>$websiteErr</span> = <span>""</span>;
<span>$name</span> = <span>$email</span> = <span>$gender</span> = <span>$comment</span> = <span>$website</span> = <span>""</span>;

<span>if</span> (<span>$_SERVER</span>[<span>"REQUEST_METHOD"</span>] == <span>"POST"</span>)
{
   <span>if</span> (<span>empty</span>(<span>$_POST</span>[<span>"name"</span>]))
     {<span>$nameErr</span> = <span>"Name is required"</span>;}
   <span>else</span>
     {
     <span>$name</span> = test_input(<span>$_POST</span>[<span>"name"</span>]);
     <span>// check if name only contains letters and whitespace</span><span>if</span> (!preg_match(<span>"/^[a-zA-Z ]*$/"</span>,<span>$name</span>))
       {
       <span>$nameErr</span> = <span>"Only letters and white space allowed"</span>; 
       }
     }

   <span>if</span> (<span>empty</span>(<span>$_POST</span>[<span>"email"</span>]))
     {<span>$emailErr</span> = <span>"Email is required"</span>;}
   <span>else</span>
     {
     <span>$email</span> = test_input(<span>$_POST</span>[<span>"email"</span>]);
     <span>// check if e-mail address syntax is valid</span><span>if</span> (!preg_match(<span>"/([\w\-]+\@[\w\-]+\.[\w\-]+)/"</span>,<span>$email</span>))
       {
       <span>$emailErr</span> = <span>"Invalid email format"</span>; 
       }
     }

   <span>if</span> (<span>empty</span>(<span>$_POST</span>[<span>"website"</span>]))
     {<span>$website</span> = <span>""</span>;}
   <span>else</span>
     {
     <span>$website</span> = test_input(<span>$_POST</span>[<span>"website"</span>]);
     <span>// check if URL address syntax is valid (this regular expression also allows dashes in the URL)</span><span>if</span> (!preg_match(<span>"/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i"</span>,<span>$website</span>))
       {
       <span>$websiteErr</span> = <span>"Invalid URL"</span>; 
       }
     }

   <span>if</span> (<span>empty</span>(<span>$_POST</span>[<span>"comment"</span>]))
     {<span>$comment</span> = <span>""</span>;}
   <span>else</span>
     {<span>$comment</span> = test_input(<span>$_POST</span>[<span>"comment"</span>]);}

   <span>if</span> (<span>empty</span>(<span>$_POST</span>[<span>"gender"</span>]))
     {<span>$genderErr</span> = <span>"Gender is required"</span>;}
   <span>else</span>
     {<span>$gender</span> = test_input(<span>$_POST</span>[<span>"gender"</span>]);}
}

<span><span>function</span><span>test_input</span><span>(<span>$data</span>)</span>
{</span><span>$data</span> = trim(<span>$data</span>);
     <span>$data</span> = stripslashes(<span>$data</span>);
     <span>$data</span> = htmlspecialchars(<span>$data</span>);
     <span>return</span><span>$data</span>;
}
<span>?></span></span><span><<span>h2</span>></span>PHP Form Validation Example<span></<span>h2</span>></span><span><<span>p</span>></span><span><<span>span</span><span>class</span>=<span>"error"</span>></span>* required field.<span></<span>span</span>></span><span></<span>p</span>></span><span><<span>form</span><span>method</span>=<span>"post"</span><span>action</span>=<span>"<?php echo htmlspecialchars($_SERVER["</span><span>PHP_SELF"]);?</span>></span>"> 
   Name: <span><<span>input</span><span>type</span>=<span>"text"</span><span>name</span>=<span>"name"</span>></span><span><<span>span</span><span>class</span>=<span>"error"</span>></span>* <span><span><?php</span><span>echo</span><span>$nameErr</span>;<span>?></span></span><span></<span>span</span>></span><span><<span>br</span>></span><span><<span>br</span>></span>
   E-mail: <span><<span>input</span><span>type</span>=<span>"text"</span><span>name</span>=<span>"email"</span>></span><span><<span>span</span><span>class</span>=<span>"error"</span>></span>* <span><span><?php</span><span>echo</span><span>$emailErr</span>;<span>?></span></span><span></<span>span</span>></span><span><<span>br</span>></span><span><<span>br</span>></span>
   Website: <span><<span>input</span><span>type</span>=<span>"text"</span><span>name</span>=<span>"website"</span>></span><span><<span>span</span><span>class</span>=<span>"error"</span>></span><span><span><?php</span><span>echo</span><span>$websiteErr</span>;<span>?></span></span><span></<span>span</span>></span><span><<span>br</span>></span><span><<span>br</span>></span>
   Comment: <span><<span>textarea</span><span>name</span>=<span>"comment"</span><span>rows</span>=<span>"5"</span><span>cols</span>=<span>"40"</span>></span><span></<span>textarea</span>></span><span><<span>br</span>></span><span><<span>br</span>></span>
   Gender:
   <span><<span>input</span><span>type</span>=<span>"radio"</span><span>name</span>=<span>"gender"</span><span>value</span>=<span>"female"</span>></span>Female
   <span><<span>input</span><span>type</span>=<span>"radio"</span><span>name</span>=<span>"gender"</span><span>value</span>=<span>"male"</span>></span>Male
   <span><<span>span</span><span>class</span>=<span>"error"</span>></span>* <span><span><?php</span><span>echo</span><span>$genderErr</span>;<span>?></span></span><span></<span>span</span>></span><span><<span>br</span>></span><span><<span>br</span>></span><span><<span>input</span><span>type</span>=<span>"submit"</span><span>name</span>=<span>"submit"</span><span>value</span>=<span>"Submit"</span>></span><span></<span>form</span>></span><span><span><?php</span><span>echo</span><span>"<h2>Your Input:</h2>"</span>;
<span>echo</span><span>$name</span>;
<span>echo</span><span>"<br>"</span>;
<span>echo</span><span>$email</span>;
<span>echo</span><span>"<br>"</span>;
<span>echo</span><span>$website</span>;
<span>echo</span><span>"<br>"</span>;
<span>echo</span><span>$comment</span>;
<span>echo</span><span>"<br>"</span>;
<span>echo</span><span>$gender</span>;
<span>?></span></span><span></<span>body</span>></span><span></<span>html</span>></span></code>
登入後複製

以上就介紹了PHP - 驗證 Name, E-mail, 和 URL,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板