W3docs

PHP Form Validation: URL and Email Inputs

Form validation is an important aspect of PHP web development, ensuring that the data entered by users is accurate and meets specific requirements before it is

Form validation is an important aspect of PHP web development, ensuring that the data entered by users is accurate and meets specific requirements before it is processed. This article will focus on validating form inputs for email and URL in PHP.

Validating Email Inputs

Email inputs can be validated using the filter_var function in PHP. This function allows you to validate a string as an email address by checking if it is in the proper format. If the email is in the correct format, the function will return true; otherwise, it will return false.

Here is an example of how to validate an email input using the filter_var function:

PHP example of how to validate an email input using the filter_var function

<?php
  $email = "[email protected]";
  if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "Invalid email format";
  } else {
    echo "Valid email format";
  }
?>

It is also possible to validate email inputs using regular expressions. A regular expression is a pattern used to match specific strings. In this case, you can use a regular expression to match the proper format of an email address.

Here is an example of how to validate an email input using a regular expression:

PHP example of how to validate an email input using a regular expression

<?php
  $email = "[email protected]";
  if (!preg_match("/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/", $email)) {
    echo "Invalid email format";
  } else {
    echo "Valid email format";
  }
?>

Note: While regular expressions can validate formats, filter_var with FILTER_VALIDATE_EMAIL or FILTER_VALIDATE_URL is generally recommended for production. It handles RFC-compliant edge cases and internationalized domain names more reliably than custom regex patterns.

Validating URL Inputs

URL inputs can be validated using the filter_var function in PHP. This function allows you to validate a string as a URL by checking if it is in the proper format. If the URL is in the correct format, the function will return true; otherwise, it will return false.

Here is an example of how to validate a URL input using the filter_var function:

PHP example of how to validate a URL input using the filter_var function

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

It is also possible to validate URL inputs using regular expressions. A regular expression is a pattern used to match specific strings. In this case, you can use a regular expression to match the proper format of a URL.

Here is an example of how to validate a URL input using a regular expression:

PHP example of how to validate a URL input using a regular expression

<?php
  $url = "https://www.example.com";
  if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $url)) {
    echo "Invalid URL format";
  } else {
    echo "Valid URL format";
  }
?>

Note: The same applies to URL validation. Built-in filters are optimized for accuracy and security.

Validation vs. Sanitization: Validation checks if data matches the expected format. It does not remove or escape dangerous characters. For security, always combine validation with sanitization (e.g., using filter_var($input, FILTER_SANITIZE_EMAIL)) before storing or using the data.

In conclusion, form validation is an important aspect of PHP web development, ensuring that user-submitted data is safe and correctly formatted before processing.

Practice

Practice

Which of the following statements about PHP form, URL, and E-mail are correct?