WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP 5 Forms - Validate E-mail and URL


In this section we will explain how to verify names, e-mails, and URLs.


PHP-Verify name

The following code will detect if the name field contains letters and spaces in a simple way. If the value of the name field is invalid, an error message will be output:

$ name = test_input ($ _ POST ["name"]);
if (! preg_match ("/^ [a-zA-Z] * $ /", $ name)) {
   $ nameErr = "Only letters and spaces are allowed";
}
Note preg_match — Perform regular expression matching.

Syntax:

int preg_match (string $ pattern, string $ subject [, array $ matches [, int $ flags]])


Search the subject string for content that matches the regular expression given by pattern. If matches is provided, it is populated with the results of the search. $ matches [0] will contain text that matches the entire pattern, $ matches [1] will contain text that matches the subpattern in the first captured parenthesis, and so on.


PHP-verification email

The following code will simply check if the e-mail address is valid. If the e-mail address is invalid, an error message will be output:

$ email = test_input ($ _ POST ["email"]);
if (! preg_match ("/([\ w \-] + \ @ [\ w \-] + \. [\ w \-] +) /", $ email)) {
   $ emailErr = "illegal email format";
}

PHP-Authentication URL

The following code will detect whether the URL address is valid (the following regular expression runs a URL with a dash: "-"). If the URL address is invalid, an error message will be output:

$website = test_input($_POST["website"]);
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
  $websiteErr = "Illegal URL address"; 
}

PHP - Verify Name, E-mail, and URL

The code looks like this:

<?php
//Define a variable and set it to null by default
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
   if (empty($_POST["name"])) {
      $nameErr = "Name is required";
      } else {
         $name = test_input($_POST["name"]);
         //Check if name contains only letters and spaces
         if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
         $nameErr = "Only letters and spaces are allowed"; 
         }
     }
   
   if (empty($_POST["email"])) {
      $emailErr = "Email is required";
   } else {
      $email = test_input($_POST["email"]);
      //Check if the email is valid
      if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
         $emailErr = "Illegal mailbox format"; 
      }
   }
     
   if (empty($_POST["website"])) {
      $website = "";
   } else {
      $website = test_input($_POST["website"]);
      //Check if the URL address is valid
     if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
         $websiteErr = "Illegal URL address"; 
      }
   }

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

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