Validate Email In Php

Validate Email Using Php


Sometimes you need to validate a email when you use php as proagraming language there are many other ways to validate a email like jquery validation is popular but Php Email validation is more secure than front end validation . To validate a email using php we require php filter_var() function .






filter_var()


filter_var() function filters the result by consuming filter provided in it

This function accept three parameters and than filter out the result as follows

filter_var( variableName , filterName , options )  the last parmeter is optional and it check each filter with possible options



FILTER_VALIDATE_EMAIL

We will use FILTER_VALIDATE_EMAIL filter so that we can filter or validate a given email

we can also check for illigle characters in email by using FILTER_SANITIZE_EMAIL filter

let's see how we will filter email. just copy the following code in your xampp htdocs folder and try to run the script

<?php

 if ( isset ( $_GET ) && !empty ( $_GET ) ){

    $email  =  $_GET['email'];
 
 
    if ( filter_var ( $email , FILTER_VALIDATE_EMAIL) ){

 
        echo "<h2>$email is a valid email.</h2>";   



      }else{


        echo "<h2>$email is not a valid email.</h2>";

   }
 }
   
?>
<form   method = "get" action="">

   <p>Enter your email address : <input type="text" name="email" ></p>
  

   <input type="Submit" value="Submit">

</form>



If have any question kindly leave a comment .