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.

Related Article
50+ PHP Interview Questions and Answers 2023

1. Differentiate between static and dynamic websites. Static Website The content cannot be modified after the script is executed The Read more

All We Need to Know About PHP Ecommerce Development

  Many e-commerce sites let you search for products, show them off, and sell them online. The flood of money Read more

PHP Custom Web Development: How It Can Be Used, What Its Pros and Cons Are,

PHP is a scripting language that runs on the server. It uses server resources to process outputs. It is a Read more

PHP Tutorial

PHP Tutorial – Complete Guide for Beginners to Advanced Welcome to the most comprehensive PHP tutorial available online at PHPOnline.in Read more

Introduction of PHP

Introduction to PHP – Learn PHP from Scratch with Practical Examples Welcome to your complete beginner's guide to PHP. Whether Read more

Syntax Overview of PHP

Syntax Overview of PHP (2025 Edition) Welcome to phponline.in, your one-stop platform for mastering PHP. This comprehensive, SEO-rich tutorial on Read more

Environment Setup in PHP

Setting Up PHP Environment (Beginner’s Guide) If you’re planning to learn PHP or start developing websites using PHP, the first Read more

Variable Types in PHP

PHP Variable Types: Complete Beginner's Guide to PHP Data Types Welcome to phponline.in, your trusted source for beginner-to-advanced level PHP Read more

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Prove your humanity: 3   +   2   =