Email Validation - Php

How to validate an email address in Php?

Snippet Code

Rate this page :
  [ 0 votes]

Use this php code to validate email. Email address validation can be done using Filter Validate Email method.

<style> .errormsg {color: red;} .errormsg2 {color: green;} </style> <?php error_reporting(0); $emailermsg = $email = $emailmsg= ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST["email"])) { $emailermsg = "Email is required"; } else { $email = $_POST["email"]; if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $emailermsg = "Invalid email format"; } else{ $emailmsg= "Email is valid"; } } } ?> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> E-mail: <input type="text" name="email" value="<?php echo $email; ?>"> <span class="errormsg"> <?php echo $emailermsg;?></span> <span class="errormsg2"> <?php echo $emailmsg;?></span> <div><input type="submit" name="submit" value="Check"></div> </form>


Ask Questions

Ask Question