Permalink
Browse files

Fix email validation regex rejects addresses with single subdomain

Per RFC 5322, e.g. user@localhost is a valid e-mail address but the
previously implemented regex rejected it.

Fixes #14453
  • Loading branch information...
dregad committed Jul 5, 2012
1 parent b88e409 commit abf661e7e3942e94ca6b13c7d92b27b120659405
Showing with 12 additions and 3 deletions.
  1. +12 −3 core/email_api.php
View
@@ -78,15 +78,24 @@
$g_phpMailer = null;
/**
- *
* Use a simple perl regex for valid email addresses. This is not a complete regex,
* as it does not cover quoted addresses or domain literals, but it is simple and
* covers the vast majority of all email addresses without being overly complex.
* @return string
*/
function email_regex_simple() {
- return "/([a-z0-9!#*+\/=?^_{|}~-]+(?:\.[a-z0-9!#*+\/=?^_{|}~-]+)*)" . # recipient
- "\@((?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)/i"; # @domain
+ static $s_email_regex = null;
+
+ if( is_null( $s_email_regex ) ) {
+ $t_recipient = "([a-z0-9!#*+\/=?^_{|}~-]+(?:\.[a-z0-9!#*+\/=?^_{|}~-]+)*)";
+
+ # a domain is one or more subdomains
+ $t_subdomain = "(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)";
+ $t_domain = "(${t_subdomain}(?:\.${t_subdomain})*)";
+
+ $s_email_regex = "/${t_recipient}\@${t_domain}/";
+ }
+ return $s_email_regex;
}
/**

0 comments on commit abf661e

Please sign in to comment.