SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Aug 2008
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Proper way and best methods to validate e-mail addresses with PHP.

    This is the validation.

    Code:
      if (empty($post["email"]))
        $errors["email"] = "Email Address can't be empty!";
      else
      {
        if (!preg_match('/[^@]+@[^.]+(\.[^.]+)+/', $post["email"]))
        {
          $errors["email"] = "Email Address is invalid!";
        }
      }
    This is how it is set up in the HTML form so that it echo when the user leave the field blank or have an invalid character.

    Code:
    <label class="email_label">Email<span class="support_form_error_message"><?php echo $errors["email"]; ?></span>
    <input type="text" name="email" id="mail" class="email_field" value="<?php echo $post["email"]; ?>" />
    </label>
    I'm very new to PHP and I tried reading some stuff on advance validation but can't grasp the concept. I am only use to the basic stuff right now.

    I tested the validation by putting this in the e-mail field and it passed validation.

    *%^!!!(())))9090@///mmn@lk.com

    How do I properly validate e-mail addresses so that an actual e-mail address is accepted and all that junk does not pass through? It even allowed two @ signs.

    Any help, explanation or tutorials will be greatly appreciated!

    Thanks everyone.

    IC

  2. #2
    SitePoint Enthusiast
    Join Date
    Dec 2003
    Location
    norway
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try one of this functions
    http://code.iamcal.com/php/rfc822/

  3. #3
    SitePoint Enthusiast stef686's Avatar
    Join Date
    Aug 2005
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not big on regular expressions but here's one I use which seems to work pretty well

    PHP Code:
    if( !preg_match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$^"$email) ) { 

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    First, the regular expressions on offer can only make a best guess that the supplied address looks like an email address. Depending on the regular expression used, there are various flavours of address which might be accepted—it is very important that you understand precisely what any individual regex allows, or not.

    The specifications for what is an email address are very wide ranging and unless you're scripting a mail server in PHP then the chances are that you really don't want to allow every single combination of "valid" email addresses. Find a pattern that suits what you want to allow, you can cover most "normal" email addresses with a fairly simple pattern (by normal I mean, user information @ domain.tld).

    If you really don't want to tackle writing/using regular expressions explicitly, you may prefer just to use a Filter (FILTER_VALIDATE_EMAIL) in your code (which uses a regex under the hood).
    Salathe
    Software Developer and PHP Manual Author.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •