PHP Form URL/Email

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

PHP Form Email Validation

There is no way to be 100% sure an e-mail address is working unless we send an e-mail there. What we usually do is check if the e-mail address syntax 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(!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 (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 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 to 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

$website = $_POST[“website”];

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

Explanation

In the above example we first assigned the POST value of URL/Link in a $website variable. After that, we compared the $website string with URL pattern using 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 learnt all about how to validate email and URL address and how to apply it using various methods in PHP.

I hope you find this article helpful 🙂

 

Leave a Reply

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

Read previous post:
mysqli connect in PHP
PHP MySQLi Connect

In this article, we will learn what is mysqli connect and how to use mysqli connect to use a MySQL...

Close