> 백엔드 개발 > PHP 튜토리얼 > PHP - 이름, 이메일, URL 확인

PHP - 이름, 이메일, URL 확인

WBOY
풀어 주다: 2016-07-30 13:30:49
원래의
1205명이 탐색했습니다.

소스코드는 다음과 같습니다.

<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으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿