PHP Form URL and E-mail Validation
Learn how to validate URL and E-mail fields in a PHP form to ensure that users provide valid information.
What is URL and E-mail Validation?
URL and E-mail validation ensures that the data provided by users in the respective fields conforms to the proper format. This is crucial for processing URLs and sending emails accurately.
Why Validate URL and E-mail?
- Data Integrity: Ensures the data is in a usable format.
- User Experience: Provides instant feedback to users for correcting input.
- Security: Helps prevent issues related to invalid or malicious input.
Basic Form Example with URL and E-mail Validation
Here’s a simple HTML form that requires a user's e-mail and website URL:
<form action="form-url-email.php" method="post">
<label for="email">E-mail:</label>
<input type="email" id="email" name="email" required>
<label for="url">Website URL:</label>
<input type="url" id="url" name="url" required>
<input type="submit" value="Submit">
</form>
Implementing URL and E-mail Validation in PHP
You can validate URL and E-mail when the form is submitted:
<?php
$emailErr = $urlErr = "";
$email = $url = "";
// Validate when the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Check if E-mail is valid
if (empty($_POST["email"])) {
$emailErr = "E-mail is required.";
} else {
$email = htmlspecialchars(trim($_POST["email"]));
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid e-mail format.";
}
}
// Check if URL is valid
if (empty($_POST["url"])) {
$urlErr = "Website URL is required.";
} else {
$url = htmlspecialchars(trim($_POST["url"]));
if (!filter_var($url, FILTER_VALIDATE_URL)) {
$urlErr = "Invalid URL format.";
}
}
}
?>
Displaying Error Messages
You can display error messages to let users know which fields are invalid:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (!empty($emailErr)) {
echo "<p class='text-red-500'>" . $emailErr . "</p>";
}
if (!empty($urlErr)) {
echo "<p class='text-red-500'>" . $urlErr . "</p>";
}
}
?>
Full Example
Here’s a complete example of a form with URL and E-mail validation:
<?php
$emailErr = $urlErr = "";
$email = $url = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["email"])) {
$emailErr = "E-mail is required.";
} else {
$email = htmlspecialchars(trim($_POST["email"]));
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid e-mail format.";
}
}
if (empty($_POST["url"])) {
$urlErr = "Website URL is required.";
} else {
$url = htmlspecialchars(trim($_POST["url"]));
if (!filter_var($url, FILTER_VALIDATE_URL)) {
$urlErr = "Invalid URL format.";
}
}
}
?>
<form action="" method="post">
<label for="email">E-mail:</label>
<input type="email" id="email" name="email" value="<?php echo $email; ?>" required>
<p class='text-red-500'><?php echo $emailErr; ?></p>
<label for="url">Website URL:</label>
<input type="url" id="url" name="url" value="<?php echo $url; ?>" required>
<p class='text-red-500'><?php echo $urlErr; ?></p>
<input type="submit" value="Submit">
</form>
Conclusion
Validating URL and E-mail fields in your PHP forms is essential for ensuring that users provide the correct format. This helps enhance user experience and prevents errors in processing the data.