PHP Form Validation Example
p2-formvalidation.php function test_input($data) {
$data = trim($data);
<!DOCTYPE HTML>
<html> $data = stripslashes($data);
<head> $data = htmlspecialchars($data);
</head> return $data;
<body> }
?>
<?php
// define variables and set to empty values
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$website = test_input($_POST["website"]);
$comment = test_input($_POST["comment"]);
$gender = test_input($_POST["gender"]);
}
PHP Forms - Required Fields
p2-formvalidation-2.php if (empty($_POST["comment"])) {
<?php $comment = "";
// define variables and set to empty values } else {
$nameErr = $emailErr = $genderErr = $websiteErr = ""; $comment = test_input($_POST["comment"]);
$name = $email = $gender = $comment = $website = ""; }
if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST["gender"])) {
if (empty($_POST["name"])) { $genderErr = "Gender is required";
$nameErr = "Name is required";
} else {
} else {
$name = test_input($_POST["name"]); $gender = test_input($_POST["gender"]);
} }
}
if (empty($_POST["email"])) {
$emailErr = "Email is required"; function test_input($data) {
} else { $data = trim($data);
$email = test_input($_POST["email"]); $data = stripslashes($data);
} $data = htmlspecialchars($data);
if (empty($_POST["website"])) { return $data;
$website = ""; }
} else { ?>
$website = test_input($_POST["website"]);
}
PHP Forms - Required Fields (cont…)
p2-formvalidation-2.php
<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
Website: <input type="text" name="website">
<span class="error"><?php echo $websiteErr;?></span>
<br><br>
Comment: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
Gender:
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
<input type="radio" name="gender" value="other">Other
<span class="error">* <?php echo $genderErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
PHP Forms - Validate E-mail and URL
p2-formvalidation-email_URL.php
<?php if (empty($_POST["email"])) {
// define variables and set to empty values $emailErr = "Email is required";
$nameErr = $emailErr = $genderErr = $websiteErr = ""; } else {
$name = $email = $gender = $comment = $website = ""; $email = test_input($_POST["email"]);
if ($_SERVER["REQUEST_METHOD"] == "POST") { // check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
if (empty($_POST["name"])) {
$nameErr = "Name is required"; }
} else { }
$name = test_input($_POST["name"]);
// check if name only contains letters and if (empty($_POST["website"])) {
$website = "";
whitespace } else {
if (!preg_match("/^[a-zA-Z-' ]*$/",$name)) {
$nameErr = "Only letters and white space allowed"; $website = test_input($_POST["website"]);
} // check if URL address syntax is valid
} if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-
9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
$websiteErr = "Invalid URL";
}
}
PHP Forms - Validate E-mail and URL (cont…)
p2-formvalidation-email_URL.php
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"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
1. preg_match() is a PHP function used for performing a regular expression match.
2. The regular expression pattern is enclosed within /.../, which checks for the validity of a URL.
3. \b asserts a word boundary, ensuring that the match starts at the beginning of a word.
4. (?:https?|ftp):\/\/ matches either "http://", "https://", or "ftp://".
5. www\. matches "www.".
6. [-a-z0-9+&@#\/%?=~_|!:,.;]*: This matches any combination of characters
commonly found in URLs after the protocol
or "www." part.
7. [-a-z0-9+&@#\/%=~_|]: This matches the last part of the URL.
8. i at the end of the pattern makes the match case-insensitive.