php email validation code example

Example 1: php filter validate email

<?php
$email_a = '[email protected]';
$email_b = 'bogus';

if (filter_var($email_a, FILTER_VALIDATE_EMAIL)) {
    echo "L'adresse email '$email_a' est considérée comme valide.";
}
if (filter_var($email_b, FILTER_VALIDATE_EMAIL)) {
    echo "L'adresse email '$email_b' est considérée comme valide.";
} else {
    echo "L'adresse email '$email_b' est considérée comme invalide.";
}
?>

Example 2: php preg_match email validation code

<?php
   function checkemail($str) {
         return (!preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
   }
   if(!checkemail("[email protected]")){
      echo "Invalid email address.";
   }
   else{
      echo "Valid email address.";
   }
?>

Example 3: valide email php

$email = "[email protected]";

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  $emailErr = "Email invalide";
}

Example 4: how to validate an email field using php

<?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");
   }
?>

Example 5: check mail php

filter_var($email, FILTER_VALIDATE_EMAIL)

Example 6: email validation php

// E-mail Code Validation using a four digit number: 
// var.inc.php: 
<?php 
session_start();
$x = mt_rand(1000,9999);
// index.php: 
<?php 
include_once 'var.inc.php';
$_SESSION['key'] = $x;
if(isset($_POST['submit'])){
    if(!mail($_POST['email-in'], "Verify", "Code: ". $x)){
        echo "ERROR EMAIL";
    }else{
        header("Location: validate.php");
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Validate email</title>
</head> 
<body>
    <form action="" method="post">
        <input type="email" placeholder="email" name="email-in">
        <button id="submit" type="submit" name="submit">Submit</button>
    </form>
</body>
</html>
// validate.php: 
  <?php session_start();?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Validate</title>
</head>
<body>
    <form action="" method="post">
        <input type="number" name="user-key">
        <button type="submit" name="submit-user-key">Validate</button>
    </form>
</body>
</html>
<?php 
    if(isset($_POST['submit-user-key'])){
        if($_POST['user-key'] == $_SESSION['key']){
            //Do something
        }
    }
?>