PHP Validate Email – String Function

<?php
   function validate_email($email) {
      $regexp = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,6})$";      if (eregi($regexp, $email)) return 1;
         else return 0;
   }   if (isset($_POST['submit'])){
      echo "Hi ".$_POST['name']."!<br />";
      if (validate_email($_POST['email']))
         echo "The address ".$_POST['email']." is valid!";
      else
         echo "The address <strong>".$_POST['email']."</strong> is invalid!";
   }
?><form action="index.php" method="post">
   Name:<input type="text" name="name" value="" />
   Email Address:<input type="text" name="email" value="" />
   <input type="submit" name = "submit" value="Go!" />
</form>

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.