PHP Form Validation With Regular Expressions Example
Teams applying “PHP Form Validation With Regular Expressions Example” in a production project can use the linked project resource to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the PHP project and test it against the project’s actual database and runtime.
Jun 18, 2021
Hi guys,
Today i will explained how to use php form validation with Regular Expressions in your form. This example is so easy to use in php. This example to i am a create a bootstrap form and set to the required validation & Regular Expressions validation in your form input.
This example to i am create a name,email,website,comment,and gender field and set to the required validation & Regular Expressions validation in php.
So let's start to the example.
Example :
index.php
<?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"]);
// check if name only contains letters and whitespace
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"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["website"])) {
$websiteErr = "website is required";
} else {
$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";
}
}
if (empty($_POST["comment"])) {
$commentErr = "Comment is required";
} 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;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Form Validation With Regular Expressions Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<style>
.error {
color: #FF0000;
}
</style>
</head>
<body>
<div class="container">
<h2>PHP Form Validation With Regular Expressions Example</h2>
<p><span class="error">* All Fields required</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="form-group">
<label for="name">Name:<span class="error">*</span></label>
<input type="text" class="form-control" id="name" placeholder="Enter Name" name="name">
<span class="error"><?php echo $nameErr;?></span>
</div>
<div class="form-group">
<label for="email">Email:<span class="error">*</span></label>
<input type="email" class="form-control" id="email" placeholder="Enter email" name="email">
<span class="error"><?php echo $emailErr;?></span>
</div>
<div class="form-group">
<label for="website">Website:</label>
<input type="website" class="form-control" id="website" placeholder="Enter Website" name="website">
<span class="error"><?php echo $websiteErr;?></span>
</div>
<div class="form-group">
<label>Comment:</label>
<textarea name="comment" class="form-control" rows="5" cols="40"></textarea>
<span class="error"><?php echo $commentErr;?></span>
</div>
<label>Gender:<span class="error">*</span></label><br>
<label class="radio-inline">
<input type="radio" name="gender" value="male"> Male
</label>
<label class="radio-inline">
<input type="radio" name="gender" value="female"> Female
</label>
<label class="radio-inline">
<input type="radio" name="gender" value="other"> Other
</label><br>
<span class="error"><?php echo $genderErr;?></span>
<br>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
<div class="container mt-3 mb-3">
<div class="row">
<div class="col-md-12">
<?php
echo "<h2>Your Output:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>
</div>
</div>
</div>
</body>
</html>
Output
Your Output:
Evelyn Mason
[email protected]
https://tryphp.w3schools.com/
how are you.
male
now you can check your own.
- How To Use Php Conditional Statement Example
- How to Check if String Contains Regex in PHP?
- PHP Array_column() Function Example
- PHP Generate List of Random Numbers Between 0 and 100 Example
- How to User Login with Facebook in PHP?
- How to Explode Every Character into Array Using PHP Example?
- PHP Form Validation Tutorial
- PHP Convert XML to JSON Example