ホームページ > バックエンド開発 > PHPチュートリアル > PHP - 名前、電子メール、URL を検証する

PHP - 名前、電子メール、URL を検証する

WBOY
リリース: 2016-07-30 13:30:49
オリジナル
1210 人が閲覧しました

以下はソースコードです:

<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 - 名前、電子メール、URL の確認についての内容を含めて紹介しました。PHP チュートリアルに興味のある友人に役立つことを願っています。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート