Friday 27 July 2012

PHP Email validation


Email validation with Regular Expressions

This is also case-insensitive, so it will treat all characters as lower case. It is a really easy way to check the syntax and format of an email address.

<html>
<body>
<?php
if (isset($_POST['posted'])) {
   $email = $_POST['email'];
   $theresults = ereg("^[^@ ]+@[^@ ]+\.[^@ \.]+$", $email, $trashed);   //"\w+@\w+.\w+$"
   if ($theresults) {
      $isamatch = "Valid";
   } else {
      $isamatch = "Invalid";
   }
   echo "Email address validation says $email is " . $isamatch;
}
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name="posted" value="true">
Enter your email address for validation:
<input type="text" name="email" value="name@example.com">
<input type="submit" value="Validate">
</form>
</body>
</html>

No comments:

Post a Comment