Monday, July 9, 2012

PHP, validate an email address



To validate the email address in an input form in PHP,  the following regular    expression can be used:
  "/^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,6}$/i"

Example:
<?php
 if(isset($_POST['submit'])){
   /* email check */
   $email = $_POST['email'];
    if ($email == '') {
        $msg->addError('You must enter an email address.');
    } else if (!preg_match("/^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,6}$/i", $email)) {
        $msg->addError('The entered email address was invalid.');
    } 
}
?>

No comments:

Post a Comment