/    Sign up×
Community /Pin to ProfileBookmark

Regular expression for email address

Hi all Ive based my registration check on a google search but unfortunately I cant find the link to it now and im now having trouble with people registering with valid email addresses.

The type of email address Im having a problem with is

[email][email protected][/email]

However when I remove the “.” between my firstname and surname this registers correctly.

My expression is

[code=php]if (!eregi(‘^[a-zA-Z]+[a-zA-Z0-9_-]*@([a-zA-Z0-9_-]+){1}(.[a-zA-Z0-9_-]+){1,2}’, stripslashes(trim($_POST[’emailaddress’])) )) {
$errors[] = ‘<font color=”#980000″>A valid email address.</font>’;
} [/code]

Could anyone let me know where to put the additional string in and also if there are any email scenarios which may result in other users not being able to register with a valid email address.

Regards

Mike

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@ZnupiJul 18.2008 — Try using the [url=http://php.net/filter-var]filter_var()[/url] function.
Copy linkTweet thisAlerts:
@Phill_PaffordJul 18.2008 — try this:

[code=php]
$email = "[email protected]";

if(eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,6})$", $email)) {

echo "Valid email address.";
} else {

echo "Invalid email address.";
}
[/code]
Copy linkTweet thisAlerts:
@ZnupiJul 18.2008 — Why not simply
[code=php]
$email = "[email protected]";
if (filter_var($email, FILTER_VALIDATE_EMAIL)) echo "Valid email address.";
else echo "Invalid email address.";
[/code]

It seems simpler to me and it's probably even better than your patterns. For example, your patterns would say invalid email if it contained a plus sign (+) which is wrong. Email addresses can contain plus signs. Gmail has a really cool feature in which everything after the + is ignored, for example, you can email me at: [email protected] and I'd still get it, and I can also filter messages based on what's after the + sign. Right now on every site I sign up or give my email address to, I use [email protected], so I know who's spamming me ?
Copy linkTweet thisAlerts:
@Phill_PaffordJul 18.2008 — [code=php]
$email = "[email protected]";

if(eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)(+[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,6})$", $email)) {
echo "Valid email address.";
}else {
echo "Invalid email address.";
}
[/code]
Copy linkTweet thisAlerts:
@Phill_PaffordJul 18.2008 — Why not simply
[code=php]
$email = "[email protected]";
if (filter_var($email, FILTER_VALIDATE_EMAIL)) echo "Valid email address.";
else echo "Invalid email address.";
[/code]

It seems simpler to me and it's probably even better than your patterns. For example, your patterns would say invalid email if it contained a plus sign (+) which is wrong. Email addresses can contain plus signs. Gmail has a really cool feature in which everything after the + is ignored, for example, you can email me at: [email protected] and I'd still get it, and I can also filter messages based on what's after the + sign. Right now on every site I sign up or give my email address to, I use [email protected], so I know who's spamming me ?[/QUOTE]


http://lists.virus.org/sec-adv-0704/msg00107.html <-- This is why
Copy linkTweet thisAlerts:
@NogDogJul 18.2008 — I usually use the function described in [url=http://www.iamcal.com/publish/articles/php/parsing_email/]this article[/url] as the most complete implementation of the RFC I've seen.
Copy linkTweet thisAlerts:
@Phill_PaffordJul 18.2008 — nice validation script Nog. updating mine now, :-)
Copy linkTweet thisAlerts:
@NogDogJul 18.2008 — http://lists.virus.org/sec-adv-0704/msg00107.html <-- This is why[/QUOTE]
Interesting. I did some testing (under PHP 5.2.1) and found that at least if the very last character is a newline, it passes validation. However, note that the FILTER_SANITIZE_EMAIL option will strip out that newline, so you could use a 2-step process (though it's less than ideal if you prefer to outright reject everything that's not valid):
[code=php]
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
if(filter_var($mail, FILTER_VALIDATE_EMAIL) === false)
{
// reject it
}
[/code]

I suspect having to run filter_var twice will pretty much eat up any processing time advantage over a single preg_match() call, but it's still pretty clean code-wise.
×

Success!

Help @cs3mw spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...