PHP.. How to check valid email error?

by 10 comments
Hi, I can check field, number of character error. But I don't know how to check valid email error or website error..

I mean if user submit invalid email address or website like without "@" and ".com .net" then script will not run further...

How do I create this script? Please help

thanks
#programming #check #email #error #php #valid

  • Profile picture of the author nathan1
  • Profile picture of the author LordKaT
    Linux Journal has an excellent article on validating that a given e-mail address conforms to the RFC specification, check it out.
  • Profile picture of the author SteveSRS
    Hi,

    That is a lot of coding in the article above for something that can be done way easier:
    PHP: Validation - Manual

    For php questions like this always do a search on php.net almost always helps!
  • Profile picture of the author MartinPlatt
    Originally Posted by rrahman View Post

    Hi, I can check field, number of character error. But I don't know how to check valid email error or website error..

    I mean if user submit invalid email address or website like without "@" and ".com .net" then script will not run further...

    How do I create this script? Please help

    thanks
    You should be able to check it using a regular expression. Should be plenty of examples on Google...
  • Profile picture of the author dwoods
    I've used many regex's over the years -- some strict some not; but my current method for validating email with PHP is using the PHP's pre-baked filter_var()

    PHP Code:
    //best way is with php5 filters: 
    if(!filter_var(, FILTER_VALIDATE_EMAIL)){
        return 
    false;

  • Profile picture of the author TheContentAuthority
    rrahman

    You could use this,

    //email validations
    if(!$this->validate_email($_POST['email']))
    {
    $this->add_error("Please provide a valid email address");
    $ret = false;
    }

    With this function
    function validate_email($email)
    {
    return eregi("^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$", $email);
    }

    Hope that helps,

    Shawn
  • Profile picture of the author ankur625
    use this,

    if(!empty($_POST['email'])) {
    if(!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $_POST['email'])){
    $errors = "Please enter a valid email address.";
    } else { $email = mysql_real_escape_string(trim($_POST['email'])); }
    } else { $errors[] = "You forgot to enter email address."; }
  • Profile picture of the author Amirol
    Hi,

    First make sure your input type is email.

    <input type="email" size="30" value="" />

    Then for 2nd layer validation, just use the code provided by dwoods.
  • Profile picture of the author sktthemes
    Hello
    Use PHP built in functions

    if(filter_var($mail, FILTER_VALIDATE_EMAIL))
    {
    echo "Mail is valid!";
    }
  • Profile picture of the author otfromtot
    I use this in my script
    PHP Code:
     = array(
        
    'first_name' => array(
            
    'filter' => FILTER_VALIDATE_REGEXP,
            
    'options' => array('regexp' => '/^[a-z- ]{3,20}$/i'),
        ),
        
    'last_name' => array(
            
    'filter' => FILTER_VALIDATE_REGEXP,
            
    'options' => array('regexp' => '/^[a-z- ]{3,20}$/i'),
        ),
        
    'phone' => array(
            
    'filter' => FILTER_VALIDATE_REGEXP,
            
    'options' => array('regexp' => '/^[0-9-]{7,20}$/i'),
        ),
        
    'email' => FILTER_VALIDATE_EMAIL,
    ); 

Next Topics on Trending Feed