the function valid_email_address($mail)

<?php
function valid_email_address($mail) {
  $user = '[a-zA-Z0-9_\-\.\+\^!#\$%&*+\/\=\?\`\|\{\}~\']+';
  $domain = '(?:(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.?)+';
  $ipv4 = '[0-9]{1,3}(\.[0-9]{1,3}){3}';
  $ipv6 = '[0-9a-fA-F]{1,4}(\:[0-9a-fA-F]{1,4}){7}';

  return preg_match("/^$user@($domain|(\[($ipv4|$ipv6)\]))$/", $mail);
}
?>

does not tot get all the (in)valid email addresses. a better reg expression can be found on http://fightingforalostcause.net/misc/2006/compare-email-regex.php

/^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i

Is this somthing we can use and put in?

Comments

mfer’s picture

Assigned: Unassigned » mfer
Priority: Minor » Normal

Drupal 7 uses filter_var() to handle the regex and there are tests in core.

But, D7 requires php 5.2+. The regex used by filter_var() before php 5.2.14 or in the 5.3 branch before 5.3.3 would fail tests if we had them in core. Yeah, it looks like valid_email_address change went in without tests (I wrote it so you can blame me).

I'm thinking we test for the PHP version and if it has the bad regex we use the one straight out of PHP head. You can see the comment block, regex, and change at http://svn.php.net/viewvc/php/php-src/tags/php_5_2_14/ext/filter/logical...

And we should add tests. I'll add this to my todo list unless someone else wants it.

Oh, and I'm putting this here because the regex in PHP head is based on the referenced article. We just have some other versions to deal with.

One note, what is in PHP and what we are using disallows addresses like foo@localhost. There are considered invalid per the spec. This is good for Drupal because we expect email addresses to be valid and routable.

markie’s picture

Issue tags: -email regexpr

I have problem with valid_email_address(). Nothing personal, but one of my clients has an underscore in their domain name. This is not validating using the current D7 install. I had to make the following change to webform.module:

if (!preg_match("/^[a-z0-9_-]+[a-z0-9_.-]*@[a-z0-9_-]+[a-z0-9_.-]*\.[a-z]{2,5}$/",trim($email))) {
  form_set_error(
    'email_custom',
    t('The entered e-mail address "@email" does not appear valid.', array('@email' => $email))
  );
} 
bertboerland’s picture

actually user@localhost IS a valid email address, see http://drupal.org/node/308138#comment-1014414

pepekovac’s picture

Issue tags: +email regexpr

My problem with valid_email_address() (Drupal 6.24):
Do not accept as valid e-mail this format: "user@subdomain.domain-name.xy" (eg. foo@mail.t-com.sk).

My way to fix:

<?php
function valid_email_address($mail) {
...
$domain = '(?:(?:[a-zA-Z0-9]|[a-zA-Z0-9\.][a-zA-Z0-9\-]*[a-zA-Z0-9])(\.[a-zA-Z0-9]+)*)+';
...

?>
amourow’s picture

Thanks pepekovac,

this is really a problem, in this case when the hyphen "-" in the second place can never be sent.

fonant’s picture

Pepekovac's fix works for person@school.w-sussex.sch.uk too, but it allows some invalid addresses through, such as test@school.-sussex.sch.uk

fonant’s picture

Issue tags: +email regexpr

This is fixed for Drupal 6 in version 6.25 with a roll-back for the broken fix mentioned here: #12274: Do not validate email addresses with trailing periods

plazik’s picture

D7 doesn't validate email with two or more points like "test..test@test.com". This is a real problem. I upgraded site from 6 to 7 and some users can't change profile settings because email isn't validate.

traviscarden’s picture

Issue summary: View changes
Status: Active » Closed (won't fix)
Issue tags: -email regexpr