PHP Form URL/E-mail Validation

In PHP, you can validate URL and email formats using built-in functions. This is essential to ensure that the data entered by users is in the correct format.

Validating Email Addresses

<?php
$email = "user@example.com";
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "Invalid email format";
} else {
    echo "Valid email format";
}
?>

Explanation: This example uses filter_var() with FILTER_VALIDATE_EMAIL to validate the email format.

Validating URLs

<?php
$url = "https://www.example.com";
if (!filter_var($url, FILTER_VALIDATE_URL)) {
    echo "Invalid URL format";
} else {
    echo "Valid URL format";
}
?>

Explanation: This example uses filter_var() with FILTER_VALIDATE_URL to validate the URL format.