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.
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";
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.
Here's the PHP version:
Code:
'/[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+/i'
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;
}
}
Can you have two @ signs in an email address? Only, I don't think you can, and that script lets it through anyway.
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";
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
}
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";
Thanks for the tip - I'm no good with RegExps.
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?
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";
It's working now, thanks. I had just used bad syntax when I added the extra bits.
How about sharing the code that worked?
/Marcus - Newbie to PHP...
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>
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
Forum Rules
Bookmarks