Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    Regular Coder
    Join Date
    Feb 2004
    Posts
    145
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Preg Match Problem.

    Hi everyone, i have teh following function using preg_match to validate email addresses:
    PHP Code:
    function validate_email($email

      
    $email trim$email ); # removes whitespace 
      
    if((empty($email)) || (!preg_match('/^[a-z0-9\\_\\.]+@[a-z0-9\\-]+\\.[a-z]+\\.?[a-z]{2,4}$/i'$email$match))){
          return 
    1;}

    Unfortunately it thinks email addresses of the form abc@xyz.fsnet.co.uk are invalid, which they arent. I have been trying to modify it so that all forms of email addresses from 'abc@xyz.com' to 'abc@xyz.fsnet.co.uk' are allowed, but with regex being my major downfall I havent been having much luck. If anyone can quickly solve my problem I would be most grateful.

    Many Thanks,
    Andy.

  • #2
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ^[a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$

    or something like that.

  • #3
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Heres one I use for email addresses:

    if (preg_match('/[^0-9A-Za-z\_\-\.\@]/', $email)) {


    That what u looking for?

  • #4
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by cyphix
    preg_match('/[^0-9A-Za-z\_\-\.\@]/', $email)
    Bet that matches alot

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by cyphix
    Heres one I use for email addresses:

    if (preg_match('/[^0-9A-Za-z\_\-\.\@]/', $email)) {


    That what u looking for?
    Am I on crack, or do I see that the email needs to match any character that does not begin with an alphanumeric character, an underscore, a hyphen, a period, or an @ symbol?

    Lets see here....
    Hmm, give this a shot:
    PHP Code:
    if (preg_match('/^([a-z0-9_-]+)(\.[a-z0-9_-]+)*@([-a-z0-9]+\.?)+\.[a-z]{2,4}$/i'$email))
    {
         
    // Blah blah blah...

    or... if the perl delimiters work, which they should:
    PHP Code:
    if (preg_match('/^([-\w]+)(\.[-\w]+)*@([-a-z0-9]+\.?)+\.[a-z]{2,4}$/i'$email))
    {
         
    // Blah blah blah...

    Yeah, so that should work as long as the email begins with an alphanumeric character, an underscore or hyphen, followed by any amount of characters after that point seperated by a period, and ending with your server domain. Its been awhile, but I believe that email address can contain those characters, but the server cannot have the underscore, correct me if I'm wrong. Oh, I haven't tested that btw, so if you try it let me know so that I can use it too
    Last edited by Fou-Lu; 10-12-2004 at 06:32 PM.

  • #6
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Am I on crack, or do I see that the email needs to match any character that does not begin with an alphanumeric character, an underscore, a hyphen, a period, or an @ symbol?
    You're on crack..

    No, it says... if $email contains anything but those charcters then do whatever....

  • #7
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by cyphix
    You're on crack..

    No, it says... if $email contains anything but those charcters then do whatever....
    Not very accurate then

  • #8
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Quote Originally Posted by trib4lmaniac
    Not very accurate then
    How is it not accurate?

    I wanna check it's a valid email so if it contains anything but those characters then it'll produce an error.

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by cyphix
    How is it not accurate?

    I wanna check it's a valid email so if it contains anything but those characters then it'll produce an error.
    Because its not accurate. Your replacement technique specifies that if it will only match a string containing a symbol in it, excluding the underscore, hyphen, and the @ symbol.
    PHP Code:
    if (preg_match('/[^0-9A-Za-z\_\-\.\@]/'$email)) {


    This will almost always return false. Unless the forums stripped the \ in front of the ^, then its all good, and would match almost any string period. Otherwise I see little use for such a matching pattern.

    Example of what that matching will return on:
    PHP Code:
    if (preg_match('/[^0-9A-Za-z\_\-\.\@]/'$email))
    {
          return 
    true;

    The following will return valid:
    $email = '!';
    $email = '###$%***';
    Examples of what will not work correctly:
    $email = 'you@yourdomain.com';
    $email = 'yourdomain';

    I'm confused as to how this can be used to correctly validate an email.
    Last edited by Fou-Lu; 10-13-2004 at 12:51 PM.

  • #10
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    I think I see what you're saying..... now that I recall, I use this for making sure the email address doesn't contain any characters it shouldn't & only valid characters that are allowed in email addresses.

    I also use this one to check it has a '@' symbol..

    PHP Code:
    if (strpos($email"@") == false) { 

  • #11
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    The following will return valid:
    $email = '!';
    $email = '###$%***';
    Examples of what will not work correctly:
    $email = 'you@yourdomain.com';
    $email = 'yourdomain';
    Quite the contrary actually

    I'm looking for ERRORS in that code..... as explained in the above post.... not saying "IF it's alright then continue on"..... saying "IF it contains anything but these characters" then we have a problem..

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Thats the problem though, there isn't enough information to base off of. Even with the added strpos, a lot will still work, to say that its valid when it truely is not.

    $email = !@;
    will return true.

  • #13
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Yes. it will.

    But this is enough validation for me seeing as I also use email validation with a link they click in their email.... but I guess that's not exactly what we're getting at here.

  • #14
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Ah, I sees, you are pretty much validating that it has the @ in it. That makes sense then. But yeah, for the application needed, I would recommend a more extensive pattern matching technique.

  • #15
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    function validate_email($address) {
      
    $address preg_replace('/"[^"]*"/'''$address);
      return 
    preg_match('/^[a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$/i'$address);

    I think that should match pretty much all types of email around. Not tested though.


  •  

    Posting Permissions

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