PHP Form Validation - Validate Email and URL

巴扎黑
Release: 2016-11-12 09:22:11
Original
1781 people have browsed it


PHP Form Verification - Verify Email and URL

PHP Form Required

PHP Form Completion

This section shows how to verify name, email and URL.

PHP - Validate Name

The following code shows a simple way to check if the name field contains letters and spaces. If the name field is invalid, an error message is stored:

$name = test_input($_POST["name"]);

if (!preg_match("/^[a-zA-Z ]*$/",$ name)) {

$nameErr = "Only letters and spaces allowed!";

}

Note: The preg_match() function retrieves the pattern of a string and returns true if the pattern exists, otherwise it returns false.

PHP - Verify E-mail

The following code shows a simple way to check whether the e-mail address syntax is valid. Store an error message if invalid:

$email = test_input($_POST["email"]);

if (!preg_match("/([w-]+@[w-]+.[w-] +)/",$email)) {

$emailErr = "Invalid email format! ";

}

PHP - Verify URL

The method shown in the following code checks whether the URL address syntax is valid (this regular expression formula also allows slashes in URLs). If the URL address syntax is invalid, an error message is stored:

$website = test_input($_POST["website"]);

if (!preg_match("/b(?:(?:https?|ftp): //|www.)[-a-z0-9+&@#/%?=~_|!:,.;]*[-a-z0-9+&@#/%

=~_| ]/i",$website)) {

$websiteErr = "Invalid URL";

}

PHP - Verify Name, Email, and URL

Now, the script looks like this:

Example

<?php
// 定义变量并设置为空值
$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"]);
    // 检查名字是否包含字母和空格
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
      $nameErr = "Only letters and white space allowed"; 
    }
  }
  if (empty($_POST["email"])) {
    $emailErr = "Email is required";
  } else {
    $email = test_input($_POST["email"]);
    // 检查电邮地址语法是否有效
    if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
      $emailErr = "Invalid email format"; 
    }
  }
  if (empty($_POST["website"])) {
    $website = "";
  } else {
    $website = test_input($_POST["website"]);
    // 检查 URL 地址语言是否有效(此正则表达式同样允许 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"]);
  }
}
?>
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!