YouTip LogoYouTip

Php Form Url Email

PHP Form - Validate E-mail and URL |

-- Learning is not just about technology, but also about dreams!

PHP Tutorial

PHP Forms

PHP Advanced Tutorial

PHP 7 New Features

PHP Database

PHP XML

PHP and AJAX

PHP Reference

PHP Form - Required Fields

PHP Complete Form Example

PHP Form - Validate E-mail and URL


In this chapter, we will introduce how to validate names, e-mails, and URLs.


PHP - Validate Name

The following code will use a simple way to check if the name field contains only letters and whitespace. If the name field value is invalid, an error message will be output:

$name = test_input($_POST);
if (!preg_match("/^*$/",$name)) {
    $nameErr = "Only letters and white space allowed";
}
Note

preg_match β€” Perform a regular expression match.

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

Searches subject for a match to the regular expression given in pattern. If matches is provided, then it will be filled with the results of the search. $matches will contain the text that matched the full pattern, $matches will contain the text that matched the first captured parenthesized subpattern, and so on.


PHP - Validate E-mail

The following code will use a simple way to 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);
if (!preg_match("/(+@+.+)/",$email)) {
    $emailErr = "Invalid email format";
}

PHP - Validate URL

The following code will check if the URL address is valid (the following regular expression allows dashes "-" in the URL). If the URL address is invalid, an error message will be output:

$website = test_input($_POST);
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

The code is as follows:

<?php
// Define variables and set default values to empty strings
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";

if ($_SERVER == "POST") {
    if (empty($_POST)) {
        $nameErr = "Name is required";
    } else {
        $name = test_input($_POST);
        // Check if name only contains letters and whitespace
        if (!preg_match("/^*$/",$name)) {
            $nameErr = "Only letters and white space allowed";
        }
    }

    if (empty($_POST)) {
        $emailErr = "Email is required";
    } else {
        $email = test_input($_POST);
        // Check if e-mail address is valid
        if (!preg_match("/(+@+.+)/",$email)) {
            $emailErr = "Invalid email format";
        }
    }

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

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

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

Run Example Β»

PHP Form - Required Fields

← Php Form CompletePhp Form Required β†’