Skip to content
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...
1 parent 6ec8cc6 commit 5719c35adb39f1eb06f3260dbf2618d18166090e @dregad dregad committed with dregad Jul 5, 2012
Showing with 12 additions and 3 deletions.
  1. +12 −3 core/email_api.php
View
15 core/email_api.php
@@ -70,15 +70,24 @@
require_lib( 'ezc/Base/src/base.php' );
/**
- *
* 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 5719c35

Please sign in to comment.
Something went wrong with that request. Please try again.