PHP Form URL/Email

Email and URL address form field validation is easy in PHP. There are inbuilt functions available to validate Email and URL addresses.

PHP Form Email Validation

There is no way to be 100% sure if an e-mail address is working unless we send an e-mail. What we usually do is check if the syntax of the e-mail is valid. Here there are different methods to prevent if data entered into the input field named “email” is an e-mail address without any unnecessary complications.

Must Read: PHP Form Required

Method 1: Email Validation using regular expression

<?php

// Function to validate email using regular expression

function email_validation($str) {

  return (!preg_match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$^", $str)) ? FALSE : TRUE;

}

// Function call

if (empty($_POST["email"])) {

    $emailErr = "This field is required.";

  }

else if(!email_validation("[email protected]")) {

  echo "Invalid email address.";

} else {

  echo "Valid email address.";

}

?>

Output

Valid email address.

Explanation

In the above example, passing the Email to the user-defined function email_validation( $email ) matches the regular expression using the  preg_match(). This predefined function matches the input with a regular expression and returns true or false depends on the match found.

Method 2: Email validation using filter_var() method

<?php

$email = "[email protected]";

// Validate email

if (empty($_POST["email"])) {

    $emailErr = "This field is required.";

  } else if(filter_var($email, FILTER_VALIDATE_EMAIL)) {

  echo("$email is a valid email address");

} else {

  echo("$email is not a valid email address");

}

?>

Output

[email protected] is a valid email address

Explanation

In the above example, passing the input email address to the filter_var() takes two parameters as input email, and the second is the type of email filter. The function filters the Email and returns true or false

Method 3: Email validation using FILTER_SANITIZE_EMAIL filter

<?php

// Declare variable and store it in email

$email = "[email protected]";

$email = filter_var($email, FILTER_SANITIZE_EMAIL);

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {

  echo("$email is a valid email address");

} else {

  echo("$email is not a valid email address");

}

?>

Output

[email protected] is a valid email address

Explanation

In the above example, use the FILTER_SANITIZE_EMAIL filter to remove all unsupported characters and then use the FILTER_VALIDATE_EMAIL filter to validate Email.

PHP URL Address/Link Validation

If we have an input field named “website,” we can check for a valid URL address.

The code below shows how to check if a URL address syntax is valid. If the URL address syntax is not valid, then store an error message.

Example

if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&amp;@#\/%?=~_|!:,.;]*[-a-z0-9+&amp;@#\/%=~_|]/i",$website)) {
  $websiteErr = "Invalid URL";
}

Explanation

In the above example, we first assigned the POST value of URL/Link in a $website variable. We compared the $website string with URL pattern using the preg_match() function.

Note: preg_match() function is inbuilt function for pattern matching in PHP.

Read More: PHP Complete Form Validation

Conclusion

In this article, we learned how to validate email and URL address and how to apply it using various PHP methods. E-mail is also a required field in a form. So, make sure to perform empty field validation too. 

We hope you have been able to understand the article. If you want to understand the other concepts of PHP programming, do refer to the PHP Tutorial and PHP MySQL sections. Happy Learning 🙂

 

Leave a Reply

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