Skip to content

Commit abf661e

Browse files
committedJul 6, 2012
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
1 parent b88e409 commit abf661e

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed
 

‎core/email_api.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,24 @@
7878
$g_phpMailer = null;
7979

8080
/**
81-
*
8281
* Use a simple perl regex for valid email addresses. This is not a complete regex,
8382
* as it does not cover quoted addresses or domain literals, but it is simple and
8483
* covers the vast majority of all email addresses without being overly complex.
8584
* @return string
8685
*/
8786
function email_regex_simple() {
88-
return "/([a-z0-9!#*+\/=?^_{|}~-]+(?:\.[a-z0-9!#*+\/=?^_{|}~-]+)*)" . # recipient
89-
"\@((?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)/i"; # @domain
87+
static $s_email_regex = null;
88+
89+
if( is_null( $s_email_regex ) ) {
90+
$t_recipient = "([a-z0-9!#*+\/=?^_{|}~-]+(?:\.[a-z0-9!#*+\/=?^_{|}~-]+)*)";
91+
92+
# a domain is one or more subdomains
93+
$t_subdomain = "(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)";
94+
$t_domain = "(${t_subdomain}(?:\.${t_subdomain})*)";
95+
96+
$s_email_regex = "/${t_recipient}\@${t_domain}/";
97+
}
98+
return $s_email_regex;
9099
}
91100

92101
/**

0 commit comments

Comments
 (0)
Please sign in to comment.