PHP Form - Validate E-mail and URL |
-- Learning is not just about technology, but also about dreams!
- Home
- HTML
- JavaScript
- CSS
- Vue
- React
- Python3
- Java
- C
- C++
- C#
- AI
- Go
- SQL
- Linux
- VS Code
- Bootstrap
- Git
- Local Bookmarks
PHP Tutorial
- PHP Tutorial
- PHP Introduction
- PHP Installation
- PHP Syntax
- PHP Variables
- PHP echo/print
- PHP EOF(heredoc)
- PHP Data Types
- PHP Type Comparison
- PHP Constants
- PHP String
- PHP Operators
- PHP If...Else
- PHP Switch
- PHP Arrays
- PHP Array Sort
- PHP Superglobals
- PHP While Loop
- PHP For Loop
- PHP Functions
- PHP Magic Constants
- PHP Namespaces
- PHP OOP
- PHP Quiz
PHP Forms
- PHP Forms
- PHP Form Validation
- PHP Form - Required Fields
- PHP Form - Validate E-mail and URL
- PHP Complete Form Example
- PHP $_GET Variable
- PHP $_POST Variable
PHP Advanced Tutorial
- PHP Multi-dimensional Arrays
- PHP Date
- PHP Include
- PHP File
- PHP File Upload
- PHP Cookie
- PHP Session
- PHP E-mail
- PHP Secure E-mail
- PHP Error
- PHP Exception
- PHP Filters
- PHP Advanced Filters
- PHP JSON
PHP 7 New Features
PHP Database
- PHP MySQL Introduction
- PHP MySQL Connect
- PHP MySQL Create Database
- PHP MySQL Create Table
- PHP MySQL Insert Data
- PHP MySQL Insert Multiple
- PHP MySQL Prepared Statements
- PHP MySQL Select Data
- PHP MySQL Where
- PHP MySQL Order By
- PHP MySQL Update
- PHP MySQL Delete
- PHP ODBC
PHP XML
PHP and AJAX
PHP Reference
- PHP Array
- PHP Calendar
- PHP cURL
- PHP Date
- PHP Directory
- PHP Error
- PHP Filesystem
- PHP Filter
- PHP FTP
- PHP HTTP
- PHP Libxml
- PHP Mail
- PHP Math
- PHP Misc
- PHP MySQLi
- PHP PDO
- PHP SimpleXML
- PHP String
- PHP XML
- PHP Zip
- PHP Timezones
- PHP Image Processing
- PHP RESTful
- PHP PCRE
- PHP Available Functions
- PHP Composer
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";
}
|
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);
}
}
?>
YouTip