WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP 5 Forms - Validate E-mail and URL


This section tells the best way to approve names, messages, and URLs.


PHP - Validate Name

The code underneath demonstrates a basic method to check if the name field just contains letters and whitespace. On the off chance that the estimation of the name field isn't substantial, at that point store a mistake message:

$name = test_input($_POST["name"]);
on the off chance that (!preg_match("/^[a-zA-Z ]*$/",$name)) {
  $nameErr = "Only letters and void area allowed";
}

PHP - Validate E-mail

The most effortless and most secure approach to browse whether an email address is very much framed is to utilize PHP's filter_var() function.

In the code underneath, on the off chance that the email address isn't very much framed, at that point store a mistake message:

$email = test_input($_POST["email"]);
on the off chance that (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  $emailErr = "Invalid email format";
}

PHP - Validate URL

The code beneath demonstrates an approach to check if a URL address language structure is substantial (this standard articulation likewise permits dashes in the URL). On the off chance that the URL address language structure isn't legitimate, at that point store a blunder message:

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

PHP - Validate Name, E-mail, and URL

Now, the content looks like this:

Example

<?php
//characterize factors and set to purge values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  in the event that (empty($_POST["name"])) {
    $nameErr = "Name is required";
  } else {
    $name = test_input($_POST["name"]);
   /check if name just contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
      $nameErr = "Only letters and void area allowed";
    }
  }

  if (empty($_POST["email"])) {
    $emailErr = "Email is required";
  } else {
    $email = test_input($_POST["email"]);
   / check if email address is well-formed
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $emailErr = "Invalid email format";
    }
  }

  if (empty($_POST["website"])) {
    $website = "";
  } else {
    $website = test_input($_POST["website"]);
   /check if URL address sentence structure is substantial (this ordinary articulation additionally permits dashes in the URL)
    if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
      $websiteErr = "Invalid URL";
    }
  }

  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"]);
  }
}
?>
Run precedent »

The following stage is to tell the best way to keep the structure from discharging all the info fields when the client presents the form.