PHP Forms Validate Email and URL with Examples

PHP Forms – Validate E-mail and URL

Validating email addresses and URLs in PHP forms is one of the most important steps in creating secure and user-friendly web applications. Without proper validation, your forms may accept incorrect data, leading to broken functionality, spam submissions, or even security risks.

In this guide from PHP Online, we will walk you through how to validate email and URL fields in PHP with clear explanations and examples.


Why Validate Email and URL in PHP Forms?

  • Prevents fake or incorrect data submission
  • Improves security by blocking malicious input
  • Ensures data consistency in databases
  • Reduces spam and invalid form entries
  • Enhances user experience

PHP Email Validation Using filter_var()

The easiest way to validate an email in PHP is by using the filter_var() function with the FILTER_VALIDATE_EMAIL filter.

<?php
$email = "example@email.com";

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "Valid email address.";
} else {
    echo "Invalid email address.";
}
?>

Benefits of filter_var() for Email Validation

  • Simple and efficient
  • Built-in PHP function (no extra library required)
  • Recognizes most valid email formats

PHP URL Validation Using filter_var()

Just like email validation, PHP makes URL validation easy with filter_var() and the FILTER_VALIDATE_URL filter.

<?php
$url = "https://www.phponline.in";

if (filter_var($url, FILTER_VALIDATE_URL)) {
    echo "Valid URL.";
} else {
    echo "Invalid URL.";
}
?>

Best Practices for URL Validation

  • Always use HTTPS URLs when possible
  • Reject suspicious or malformed links
  • Store validated URLs only in the database

Common Mistakes in PHP Form Validation

  • Not trimming whitespace before validation
  • Accepting input without sanitization
  • Forgetting to validate optional fields
  • Relying only on JavaScript validation (server-side is crucial)

Complete Example: Validate Email and URL Together

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $email = trim($_POST["email"]);
    $url = trim($_POST["url"]);

    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        echo "Invalid Email Address.<br>";
    } else {
        echo "Valid Email: $email <br>";
    }

    if (!filter_var($url, FILTER_VALIDATE_URL)) {
        echo "Invalid URL.<br>";
    } else {
        echo "Valid URL: $url <br>";
    }
}
?>

Example Form

<form method="post" action="">
  Email: <input type="text" name="email"><br>
  Website: <input type="text" name="url"><br>
  <input type="submit">
</form>

Related Tutorials on PHP Online

PHP form validation, PHP validate email, PHP validate URL, PHP forms tutorial, PHP input validation, secure PHP forms, PHP examples

Frequently Asked Questions (FAQ)

Q1: Why is server-side validation more important than client-side validation?
A1: Client-side validation can be bypassed easily, but server-side validation ensures data is validated securely on the backend.

Q2: Can I use regex for email validation instead of filter_var()?
A2: Yes, regex can be used, but filter_var() is recommended as it is more reliable and built-in.

Q3: Does PHP validation stop spam completely?
A3: No, but it reduces invalid entries. For complete spam protection, use CAPTCHA and server-side checks.

Q4: How do I validate both email and URL in one PHP form?
A4: You can use filter_var() for both fields inside the same form submission (see example above).

Q5: Is filter_var() enough for production use?
A5: For most cases, yes. However, combining it with sanitization and additional security measures is best practice.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments