/    Sign up×
Community /Pin to ProfileBookmark

email validation

hello
I have following function for email validation:

[CODE]
function is_valid_email($email) {
return preg_match(‘#^[a-z0-9.!#$%&’*+-/=?^_`{|}~]+@([0-9.]+|([^s]+.+[a-z]{2,6}))$#si’, $email);
}

[/CODE]

but I want prevent user to enter www. in front of email address
what extra regular expression do I need?

Thank in advance

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 30.2011 — Frankly, I think it's unnecessary effort to validate that the email is in a specific format, as that does nothing to verify that it is (a) an actual email address or (b) belongs to the person submitting it. But if you feel it is necessary, the most thorough implementation I've seen that will not generate false negatives is http://www.iamcal.com/publish/articles/php/parsing_email/.
Copy linkTweet thisAlerts:
@ma3743authorJun 07.2011 — Thank you nogdog

I know this is unnecessary but I have some ... users that don't know what is email format and insert someting like [email protected]
Copy linkTweet thisAlerts:
@NogDogJun 07.2011 — Thank you nogdog

I know this is unnecessary but I have some ... users that don't know what is email format and insert someting like [email protected][/QUOTE]


Which is a valid email address syntax (even if you're pretty sure 99% of the time it will be wrong ? ), so the link I gave you won't help you detect it. You could throw in an additional check to see it starts with "www." and ask the user if they really meant that, I suppose?

[code=php]
if(stripos($string, 'www.') === 0) {
// are you sure?
}
[/code]
Copy linkTweet thisAlerts:
@ma3743authorJun 08.2011 — there is no regular expression Rule?
Copy linkTweet thisAlerts:
@OctoberWindJun 08.2011 — There is, but as NogDog said, an email address like with [noparse][email protected][/noparse] is still a valid email address.

adding [B][noparse][^www.][/noparse][/B] will cause the regex to "return false" if the string starts with "www.", but in order to be 'nice' and let the user know that "www." is not your assumed valid email address, you'd still need to perform that additional check.
Copy linkTweet thisAlerts:
@NogDogJun 08.2011 — there is no regular expression Rule?[/QUOTE]

The testing for a "www." at the beginning of the email does not require the extra processing that a regular expression check would use. Therefore I was thinking of keeping it separate from any format check you might otherwise be doing, since that is a separate issue. (It is entirely "legal" for someone to have an email address that begins with "www." -- maybe their name is William Walter Winston Smith III, and they created an address of "[email protected]".) Testing for the "www." separately gives you the opportunity to handle it differently than an illegal format, such as by by providing an "are you sure?" screen before final form entry, rather than outright rejecting what might, in fact, be their correct email address and then you losing a customer due to their frustration or outright inability to submit your form.
Copy linkTweet thisAlerts:
@chrisecclesJun 09.2011 — 

I agree with NogDog.
-


You have to play *nice* with people when it comes to forms.

Sometimes, this means allowing the very slight possibility that

some eejit will spend time trying to figure how to inject a

harmful piece of code into your cgi through an email field.

You could protect (adequately, within the bounds of reason)

against this by restricting the number of form submissions

allowed before your script bombs them out.

Genuine customers will [I]want[/I] their email address to be correct

syntactically because they [I]want[/I] your service/product. Catch

their typos with a polite asterisk next to the field but don't

penalize them for having a zany email handle !

Just my 2¢.

Chris
Copy linkTweet thisAlerts:
@ma3743authorJun 09.2011 — thank you very much dear friends
Copy linkTweet thisAlerts:
@chrisecclesJun 10.2011 — One method I often employ is a lovely (but certainly not to be described

as 'compact' !) script published by Douglas Lovell in the Linux Journal

a few years back. Here is the complete listing:

[CODE]function validateEmailAddress($email) {
$isValid = true;
$atIndex = strrpos($email, "@");
if (is_bool($atIndex) && !$atIndex) {
$isValid = false;
}
else {
$domain = substr($email, $atIndex+1);
$local = substr($email, 0, $atIndex);
$localLen = strlen($local);
$domainLen = strlen($domain);
if ($localLen < 1 || $localLen > 64) {
$isValid = false;
}
else if ($domainLen < 1 || $domainLen > 255) {
$isValid = false;
}
else if ($local[0] == '.' || $local[$localLen-1] == '.') {
$isValid = false;
}
else if (preg_match('/\.\./', $local)) {
$isValid = false;
}
else if (!preg_match('/^[A-Za-z0-9\-\.]+$/', $domain)) {
$isValid = false;
}
else if (preg_match('/\.\./', $domain)) {
$isValid = false;
}
else if(!preg_match('/^(\\.|[A-Za-z0-9!#&#37;&`_=\/$'*+?^{}|~.-])+$/', str_replace("\\","",$local))) {
if (!preg_match('/^"(\\"|[^"])+"$/', str_replace("\\","",$local))) {
$isValid = false;
}
}
if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) {
$isValid = false;
}
}
return $isValid;
}
[/CODE]


Hope this is useful.

Chris

You can omit the DNS look-up in the final 'if' clause

when you need speed over accuracy !
Copy linkTweet thisAlerts:
@Jeff_MottJun 10.2011 — There's also an [url=http://www.php.net/manual/en/filter.filters.validate.php]e-mail validation filter[/url] built into PHP.

[code=php]filter_var('[email protected]', FILTER_VALIDATE_EMAIL)[/code]
Copy linkTweet thisAlerts:
@NogDogJun 10.2011 — There's also an [url=http://www.php.net/manual/en/filter.filters.validate.php]e-mail validation filter[/url] built into PHP.

[code=php]filter_var('[email protected]', FILTER_VALIDATE_EMAIL)[/code][/QUOTE]


Don't know if they've fixed it in recent versions, but it used to let things like a terminal newline through -- which is easily "fixed" by trim()-ing the value first.
×

Success!

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