#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Location
    Santa Catarina
    Posts
    2
    Rep Power
    0

    Exclamation What's wrong with this PHP code of my website


    I'm using preg_match to validate inputs in the send email form of my website like this


    Code:
    if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{3})$/i",$_POST["email"])) {
    
    	$erro .= '<li>The field "E-mail" is invalid to empresasite.com.br;</li>';
    
    }
    When I have emails tha finish with .com.br or just .br for exemplo the validations is not working, can somebody help me?
  2. #2
  3. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Originally Posted by louissantos
    I'm using preg_match to validate inputs in the send email form of my website like this


    Code:
    if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{3})$/i",$_POST["email"])) {
    
    	$erro .= '<li>The field "E-mail" is invalid to empresasite.com.br;</li>';
    
    }
    When I have emails tha finish with .com.br or just .br for exemplo the validations is not working, can somebody help me?
    (\.[a-z]{3})$

    That part is requiring that the final three characters of the string have to be a-z. You can make it {2,3} to allow a 2-character TLD, but it still wouldn't accept "just .br" due to other parts of the regex.

    I found this online that might be an alternative starting point:
    Code:
    ^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)$
    One of the problems we're facing is that they keep adding new TLDs and not all our bound to 2-3 characters. .museum is now up to 6. Thus it's becoming more difficult to control the input.

    You can use {2,6} to catch it, but that might also catch a lot of other bogus data, so the above regex is listing TLDs explicitly. The downside is that'll need to maintain it as new ones are added.
    LinkedIn: Dave Mittner
  4. #3
  5. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,549
    Rep Power
    2337
    Why not just use filter_var with FILTER_VALIDATE_EMAIL?
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  6. #4
  7. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Originally Posted by ptr2void
    Why not just use filter_var with FILTER_VALIDATE_EMAIL?
    Huh. Now where has that been all my life...
    LinkedIn: Dave Mittner
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    832
    Rep Power
    496
    The truth is that it is basically impossible to validate an e-mail address with a regular expression (or even a set of regular expressions).

    At best, you will be able to reject completely bogus inputs, such as "-@_", but forget about anything near true validation
  10. #6
  11. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,127
    Rep Power
    4304
    Welcome to DevShed Forums, louissantos.

    That expression will block more valid addresses than just those from a domain that uses a country-code TLD.

    I recommend you read these:
    http://en.wikipedia.org/wiki/Email_address
    http://isemail.info/about
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).

IMN logo majestic logo threadwatch logo seochat tools logo