PHP Form URL/Email


Validating URLs and email addresses is a critical part of web development. It ensures that users submit correctly formatted information, which can then be stored, processed, or used for communication securely. Without proper validation, forms may collect incomplete, incorrect, or even malicious data.

Why URL and Email Validation is Important

  • Prevent invalid submissions – Users cannot enter incomplete or malformed email addresses or URLs.

  • Ensure reliable communication – Correct emails are essential for newsletters, notifications, or confirmations.

  • Maintain security – Prevents malicious scripts or links being submitted in URL fields.

  • Improve user experience – Users are notified immediately if the input is invalid.

  • Reduce errors in processing – Clean data avoids backend issues when storing or sending emails.

For example, Vicky wants to submit her email and portfolio website for a newsletter subscription. If the form allows incorrect entries, she may never receive the confirmation or updates.

Accessing Form Data in PHP

PHP retrieves submitted form data using superglobals:

<?php
$email = $_POST['email'];
$website = $_POST['website'];
?>
  • $email and $website store the user input from the form.

  • This data should be validated before displaying, storing, or sending.

Basic Email Validation Using filter_var()

<?php
$email = $_POST['email'];

if (empty($email)) {
    echo "Email is required.<br>";
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "Invalid email format.<br>";
} else {
    echo "Email $email is valid.<br>";
}
?>
  • Sanjana enters sanjanaexample.com without @ and receives an error.

  • Correct emails like sanjana@example.com pass validation.

  • This simple validation prevents users from submitting malformed emails.

URL Validation Using filter_var()

<?php
$website = $_POST['website'];

if (empty($website)) {
    echo "Website URL is required.<br>";
} elseif (!filter_var($website, FILTER_VALIDATE_URL)) {
    echo "Invalid URL format.<br>";
} else {
    echo "URL $website is valid.<br>";
}
?>
  • Vrinda submits her portfolio link as https://vrindaportfolio.com and it passes validation.

  • URLs like vrindaportfolio without https:// are flagged as invalid.

  • Ensures that all submitted links are usable.

HTML Form for URL and Email

<form action="validate_process.php" method="post">
    Name: <input type="text" name="name" required><br>
    Email: <input type="email" name="email" required><br>
    Website: <input type="url" name="website" required><br>
    <input type="submit" value="Submit">
</form>
  • HTML5 input types (email and url) provide basic client-side validation.

  • Server-side PHP validation is still required to prevent bypassing checks.

  • Vicky, Sanjana, and Vrinda can submit their forms and immediately see browser warnings if the input is invalid.

Combining Required Fields with URL and Email Validation

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$website = $_POST['website'];
$errors = [];

if (empty($name)) {
    $errors[] = "Name is required.";
}
if (empty($email)) {
    $errors[] = "Email is required.";
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $errors[] = "Invalid email format.";
}
if (empty($website)) {
    $errors[] = "Website URL is required.";
} elseif (!filter_var($website, FILTER_VALIDATE_URL)) {
    $errors[] = "Invalid URL format.";
}

if (!empty($errors)) {
    foreach ($errors as $error) {
        echo $error . "<br>";
    }
} else {
    echo "Hello $name! Your email $email and website $website are valid.";
}
?>
  • Collecting errors in an array allows multiple feedback messages at once.

  • Enhances user experience and reduces frustration.

  • Vicky submits incomplete or incorrectly formatted data and sees all issues clearly.

Advanced Validation: Multiple Emails and Optional Fields

Sometimes forms allow users to enter multiple emails or leave certain fields optional:

<?php
$emails = $_POST['emails']; // Array of emails

foreach ($emails as $email) {
    if (!empty($email) && filter_var($email, FILTER_VALIDATE_EMAIL)) {
        echo "Valid email: $email<br>";
    } elseif (!empty($email)) {
        echo "Invalid email: $email<br>";
    }
}
?>
  • Users can submit multiple friends’ emails for invitations.

  • Optional fields are checked only if the user enters data.

  • Ensures flexibility while maintaining validation integrity.

Retaining Values After Validation Errors

<input type="text" name="name" value="<?php echo isset($_POST['name']) ? $_POST['name'] : ''; ?>">
<input type="email" name="email" value="<?php echo isset($_POST['email']) ? $_POST['email'] : ''; ?>">
<input type="url" name="website" value="<?php echo isset($_POST['website']) ? $_POST['website'] : ''; ?>">
  • Keeps user input in the form after submission.

  • Reduces frustration when correcting errors.

  • Sanjana and Vrinda can see their correctly entered fields while fixing mistakes.

Best Practices for URL and Email Validation

  1. Always validate on the server – Client-side validation can be bypassed.

  2. Use filter_var() for standard email and URL checks.

  3. Provide clear error messages – Users must know what is wrong.

  4. Retain input values – Improves user experience.

  5. Combine with required field validation – Ensures all mandatory fields are completed.

  6. Consider regular expressions – For stricter control over email formats.

  7. Test multiple scenarios – Include optional fields, multiple emails, and unusual URLs.

Summary of the Tutorial

  • Email and URL validation is essential for data integrity, security, and usability.

  • PHP’s filter_var() simplifies validation for standard formats.

  • HTML5 input types can provide basic client-side checks, but server-side validation is mandatory.

  • Combining required fields, retaining user input, and providing clear feedback enhances user experience.

  • Proper validation ensures that submissions from Vicky, Sanjana, Vrinda, and other users are complete, correctly formatted, and safe for processing.


Practice Questions

  1. Create a form with Name, Email, and Website fields and validate that all fields are required.

  2. Validate that the Email field contains a properly formatted email address.

  3. Validate that the Website field contains a properly formatted URL.

  4. Combine required field checks with email and URL validation and display all errors at once.

  5. Retain the values of all fields after a validation error.

  6. Create a form that allows submitting multiple email addresses and validate each one individually.

  7. Make the Website field optional but validate it only if a value is entered.

  8. Use a regular expression to validate stricter email formats.

  9. Display custom error messages for Name, Email, and Website fields separately.

  10. Create a form where multiple users (Vicky, Sanjana, Vrinda, etc.) can submit their emails and websites, and validate all submissions.


Go Back Top