www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 25

Thread: Great email verification

  1. #1
    Join Date
    May 2003
    Posts
    16

    Great email verification

    Thought I'd share this with everyone, I wrote it the other day and I like it a lot:
    function verifyemail($anchormail,$anchordescription = " ")
    {
    $domain = explode("@",$anchormail);
    if (ereg("^[a-zA-Z0-9_\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$", $anchormail)
    && ereg("^[a-zA-Z0-9_\@\.\,\ ]+$", $anchordescription)
    && checkdnsrr($domain[1]) ) {
    return TRUE;
    } else {
    return FALSE;
    }
    }
    Notice the checkdnsrr($domain[1])
    it actually looks for MX records on the domain... really cool.

  2. #2
    Join Date
    Nov 2002
    Location
    NY, USA
    Posts
    731
    d-lewart@uiuc.edu: a valid address that would fail
    ...@yahoo.com: an invalid address that would pass

    Here's a regex derived from RFC822: Standard for ARPA Internet Text Messages. Obviously more complex, but nothing short of perfect.
    Code:
    /[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+/
    This regex was tested in Perl, so some characters preceeded with a backslash (such as @) may not need be in PHP.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  3. #3
    Join Date
    May 2003
    Posts
    16
    Code:
    <?
    	function email($emailAddress,$emailDisplay = " ")
    	{      
    		if (preg_match("/[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+/", $emailAddress)){
            		$emailArray = explode("@",$emailAddress);
             		if (checkdnsrr($emailArray[1])){
                			return TRUE;
            		}
    		}
    		return FALSE; 
    	}
    ?>
    this didn't work.... :-/ I can't seem to figure out what's wrong. I tried changing all \\" to \" ..... no luck.

  4. #4
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    Here's the PHP version:

    Code:
    '/[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+/i'

    Personal website http://www.ryanbrill.com/
    Business website: http://www.infinitywebdesign.com/
    TypeSpace http://www.typespace.org/

    I reject your reality and substitute it with my own!

  5. #5
    Join Date
    May 2003
    Posts
    16
    Thanks! Excellent.


    Code:
    class verify
    {
    
     
    	function email($emailAddress)
    	{      
    		if (preg_match('/[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+/i', $emailAddress)){
            		$emailArray = explode("@",$emailAddress);
             		if (checkdnsrr($emailArray[1])){
                			return TRUE;
            		}
    		}
    		return FALSE; 
    	}
    }

  6. #6
    Join Date
    Feb 2003
    Location
    Britain
    Posts
    1,335
    Can you have two @ signs in an email address? Only, I don't think you can, and that script lets it through anyway.

  7. #7
    Join Date
    Nov 2002
    Location
    NY, USA
    Posts
    731
    Could you provide a more specific example of where you think it fails?

    Note also that if you match against a string such as "hello@world@some.thing" that what really is being matched is...

    hello@world@some.thing

    This is the valid address it finds and thus returns true. You'll have to specify that the match must start and end from the beginning to the end of the string. That's done with the ^ and $ characters, e.g.,
    Code:
    /^...regex here...$/
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  8. #8
    Join Date
    Feb 2003
    Location
    Britain
    Posts
    1,335
    Can't you check that only one @ sign is being used by this code:

    PHP Code:
    $emailArray explode("@",$emailAddress);
                     if (
    checkdnsrr($emailArray[2])){
    //Fail Code
    }else {//Succeed code


  9. #9
    Join Date
    Nov 2002
    Location
    NY, USA
    Posts
    731
    Can't you check that only one @ sign is being used by this code
    No, actually. Consider blah@blah@blah.blah
    Your conditional would then be checking checkdnsrr("blah.blah"), which would return false and execute your succeed code.

    But if you make the change I recommended then there's no need to perform the additional check at all.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  10. #10
    Join Date
    Feb 2003
    Location
    Britain
    Posts
    1,335
    Thanks for the tip - I'm no good with RegExps.

  11. #11
    Join Date
    Feb 2003
    Location
    Britain
    Posts
    1,335
    I tried the redexp with the ^ and $ thing, and it just gives the error:
    Warning: Unknown modifier '$' in /home/evernet/public_html/register.php.
    With a line number, obviously.
    I would like to use your regExp to validate email addresses on my site, could you help me please?

  12. #12
    Join Date
    Nov 2002
    Location
    NY, USA
    Posts
    731
    I can't tell you where it might have gone wrong unless I see what you tried.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  13. #13
    Join Date
    Feb 2003
    Location
    Britain
    Posts
    1,335
    It's working now, thanks. I had just used bad syntax when I added the extra bits.

  14. #14
    Join Date
    Apr 2003
    Location
    Sweden
    Posts
    54
    How about sharing the code that worked?

    /Marcus - Newbie to PHP...

  15. #15
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    Are you talking the complete code or the regexp? Here is the regexp that will work:

    Code:
    /^[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+$/i
    and here some complete code:

    Code:
    <?PHP
    
    if ($_POST['submit']) {
    	if (preg_match('/^[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+$/i', $_POST['email'])) {
    		echo ("valid");
    	}
    	else {
    		echo ("invalid");
    	}
    }
    
    ?>
    
    <html>
    <head>
    <title>Validate Email</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    
    <body>
    
    <form name="myform" method="post" action="regexp.php">
    
    <input type="text" name="email"/>
    <input type="submit" name="submit" value="Submit"/>
    
    </form>
    
    </body>
    </html>

    Personal website http://www.ryanbrill.com/
    Business website: http://www.infinitywebdesign.com/
    TypeSpace http://www.typespace.org/

    I reject your reality and substitute it with my own!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles