homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

REGEX email match
Only match if there is something supplied

 2:08 am on Jul 19, 2007 (gmt 0)

I want to create a regex to use with preg_match() to validate an email string, but I want it to also pass the match if the string is empty.

$string="test@example.com"; --> pass
$string="foo@example.com"; --> pass
$string=""; --> pass

Of course:
$string="testexample.com"; --> fail
$string="@example.com"; --> fail
$string=" "; --> fail

Can anyone help me?



 11:25 am on Jul 19, 2007 (gmt 0)

play with the function below :)

function email($email) {
// <snip>
// First, we check that there's one @ symbol, and that the lengths are right
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
return false;
// Split it into sections to make life easier
$email_array = explode("@", $email);
if(sizeof($email_array) > 2){
return false;
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{¦}~-][A-Za-z0-9!#$%&'*+/=?^_`{¦}~\.-]{0,63})¦(\"[^(\\¦\")]{0,62}\"))$", $local_array[$i])) {
return false;
if (!ereg("^\[?[0-9\.]+\]?$", $email_array)) { // Check if domain is IP. If not, it should be valid domain name
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])¦([A-Za-z0-9]+))$", $domain_array[$i])) {
return false;
return true;

[1][edited by: dreamcatcher at 1:20 pm (utc) on July 19, 2007]
[edit reason] no urls as per T.O.S [webmasterworld.com].Thanks [/edit]


 11:43 am on Jul 19, 2007 (gmt 0)

Could you explain why empty() is not good enough
why do not you carry on if is empty (if as it seems email is not a *required field)
if not then regex it

or I do not read you well :)

Pharion is that () taking care of empty str? I might (am not regex expert) be wrong, but it does not look like it considers "empty str" as ok.


 12:01 pm on Jul 19, 2007 (gmt 0)

phparion - thanks for that function...

...however, due to programming constraints, I need to do this using a single REGEX in a single preg_match()...

The requirement is that it is either EMPTY or a Valid Email Address. I'm not too fussy about the precise validation of the email address - just a general one for something which could be valid is good enough for me.

Hopefully some kind of alternation of pattern will work? Any ideas?


 12:43 pm on Jul 19, 2007 (gmt 0)

G for regexlib


 3:51 pm on Jul 19, 2007 (gmt 0)

You can likley accomplish your goal in the same regex if you use an alternative pattern (vertical bar [php.net])

You could also write your own function (or use one that already exists [webmasterworld.com]) and modify it to check for the empty string OR the regular expression and return TRUE or FALSE accordingly.

// handle empty string in the function itself 
if (isValidInetAddress($email)) {
... or you could do ...
// handle empty string here
if (!$email ¦¦ isValidInetAddress($email)) {


 1:16 pm on Jul 20, 2007 (gmt 0)

function valid_email($email) {
if (trim($email) == "") {
$bad = 0;
} else {
// based on RFC822 (1982)
$qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
$dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
$atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
$quoted_pair = '\\x5c[\\x00-\\x7f]';
$domain_literal = "\\x5b($dtext¦$quoted_pair)*\\x5d";
$quoted_string = "\\x22($qtext¦$quoted_pair)*\\x22";
$domain_ref = $atom;
$sub_domain = "($domain_ref¦$domain_literal)";
$word = "($atom¦$quoted_string)";
$domain = "$sub_domain(\\x2e$sub_domain)*";
$local_part = "$word(\\x2e$word)*";
$addr_spec = "$local_part\\x40$domain";
$bad = preg_match("!^$addr_spec$!", $email);
return ($bad == 0)? TRUE : FALSE;

[edited by: coopster at 1:59 pm (utc) on July 20, 2007]
[edit reason] removed link [/edit]


 1:42 pm on Jul 20, 2007 (gmt 0)

You can likley accomplish your goal in the same regex if you use an alternative pattern (vertical bar)

That's what I think I'm looking for, but I can't get that to work properly. Can someone give me an example of using the alternation to choose between 'exactly nothing' and 'some other pattern'?

chuckstarks - thanks for that function, however I need to do it with one single regular expression


 2:55 pm on Jul 20, 2007 (gmt 0)

The latter portion would just be an empty pattern
if (preg_match('/^(mypattern¦)$/', trim($email))) { 
print "good";
} else {
print "bad";

The forum breaks the pipe symbol so don't forget that if you are copying/pasting the email pattern in the original link I left earlier)

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved