Home > Article > Backend Development > PHP - Validate Name, E-mail, and URL
The following is the source code:
<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>
The above has introduced PHP - verifying Name, E-mail, and URL, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.