1. Sanitizing and validating email syntax
    $email = $_POST['email']; //getting the posted email
    //sanitizing the email using FILTER_SANITIZE_EMAIL
    //removes all illegal e-mail characters from a string
    $email=filter_var($email, FILTER_SANITIZE_EMAIL);
    //validating the email using FILTER_VALIDATE_EMAIL
    //validates e-mail returning true if valid and false if invalid
    (filter_var($field, FILTER_VALIDATE_EMAIL)) ? TRUE : FALSE;
    
  2. Creating a function to validate and sanitize
    function check($email)  {
         $field=filter_var($field, FILTER_SANITIZE_EMAIL);
         return (filter_var($field, FILTER_VALIDATE_EMAIL)) ? TRUE : FALSE;
    }
    

    Usage:

    $email = $_POST['email'];
    if (check($email)==TRUE){
    //send email
    }else{
    echo "Invalid email!";
    }
    
Validating emails with php
Tagged on:     

Leave a Reply

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