/    Sign up×
Community /Pin to ProfileBookmark

validating an email

i have this code to validate an email

[code=php]if (preg_match(“/^[w-.]+@[w-.]+.[w-]+$/i”,$str))[/code]

but the problem is when i have an email like :

[QUOTE]

[email protected]

[/QUOTE]

in this case the
check fails!!!!!
how to i handle the case that after the @ can be a case of 1 dot or of 2 dots?
thnaks in advance
peleg

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@chrysJun 07.2005 — Here is the best, the ultimate, regexp for e-mail checking. I use it everywhere.

/^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+-+)|([A-Za-z0-9]+.+)|([A-Za-z0-9]+++))*[A-Za-z0-9]+@((w+-+)|(w+.))*w{1,63}.[a-zA-Z]{2,6}$/
Copy linkTweet thisAlerts:
@pelegk1authorJun 07.2005 — firtst thnaks alot!

second can u explain a little what going there ?
Copy linkTweet thisAlerts:
@chrysJun 07.2005 — Not a chance ?

Basically, all of these domains will come up as invalid:

email

[email protected]

email@address

And these valid:

[email][email protected][/email]

[email][email protected][/email]

[email][email protected][/email]

Basically, it's the best validator I've seen. I got it from a site a long time ago, don't remember where... actually, let me look in my code comments for I think I put a note there.

AH! Here it is: [URL=http://www.regexlib.com/REDetails.aspx?regexp_id=295]http://www.regexlib.com/REDetails.aspx?regexp_id=295[/URL]
Copy linkTweet thisAlerts:
@ShrineDesignsJun 07.2005 — try/^[a-zA-Z0-9-_x7f-xff]+@[a-zA-Z0-9-_x7f-xff]+(.[a-zA-Z0-9x7f-xff]+)+$/
Copy linkTweet thisAlerts:
@NogDogJun 07.2005 — From http://iamcal.com/publish/articles/php/parsing_email/pdf/ (well worth reading):
[code=php]
function is_valid_email_address($email) {
$qtext = '[^\x0d\x22\x5c\x80-\xff]';
$dtext = '[^\x0d\x5b-\x5d\x80-\xff]';
$atom = '[^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c'.
'\x3e\x40\x5b-\x5d\x7f-\xff]+';
$quoted_pair = '\x5c\x00-\x7f';
$domain_literal = "\x5b($dtext|$quoted_pair)*\x5d";
$quoted_string = "\x22($qtext|$quoted_pair)*\x22";
$domain_ref = $atom;
$sub_domain = "($domain_ref|$domain_literal)";
$word = "($atom|$quoted_string)";
$domain = "$sub_domain(\x2e$sub_domain)*";
$local_part = "$word(\x2e$word)*";
$addr_spec = "$local_part\x40$domain";
return preg_match("!^$addr_spec$!", $email) ? 1 : 0;
}
[/code]
Copy linkTweet thisAlerts:
@ShrineDesignsJun 07.2005 — nice one nogdog

preg_match() returns a boolean value so the ?: is not necessary
Copy linkTweet thisAlerts:
@NogDogJun 08.2005 — nice one nogdog

preg_match() returns a boolean value so the ?: is not necessary[/QUOTE]

I didn't even notice the author did that. :rolleyes:
×

Success!

Help @pelegk1 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 4.27,
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,
)...