IsValidEmail Suggestion

7 posts by 3 authors in: Forums > CMS Builder
Last Post: October 23, 2011   (RSS)

Hello,

Just an idea :)

The 'isValidEmail' built in functionality is great.

It'd be even greater if it allowed multiple email addresses.

Currently, an input such as:

email@google.com, emailtwo@google.com

returns as invalid, even though it's essentially valid.

It'd be great if the isValidEmail allowed this ;)

Cheers!

Re: [rjbathgate] IsValidEmail Suggestion

By Jason - August 8, 2011

Hi,

Thanks for your suggestion. We can look into doing this. As a work around, you can break up your list into an array and check them one at a time like this:

<?php
$emailList = "email@google.com, emailtwo@google.com";
$emailsValid = true;

foreach (explode(",", $emailList) as $email) {
$email = trim($email);
if (!isValidEmail($email)) {
$emailsValid = false;
break;
}
}

?>


At the end of this code, you can check $emailsValid to see if all the emails were valid or not.

Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] IsValidEmail Suggestion

Hey

Yeah I've used a similar workaround but has meant I've had to customise common.php which isn't ideal for upgrades :)

Thanks :)
Rob

Re: [rjbathgate] IsValidEmail Suggestion

By Jason - August 9, 2011

Hi Rob,

You wouldn't have to customize common.php in order to be able to do this, as you can add this code into your pages. Better still, you can write a function for this and store it in a plugin, so you would have access to the function from any of your pages.

Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] IsValidEmail Suggestion

true, but I was using the inbuilt mailing function :)

all good, thanks

Re: [rjbathgate] IsValidEmail Suggestion

By Dave - October 21, 2011

Rob,

We've updated this for 2.13 (beta coming soon).

The function now supports both "display names" in emails and multiple emails. To allow multiple emails just pass a second argument of: true

$emails = "email@google.com, emailtwo@google.com";
$result = isValidEmail($emails, true);


And here's the function code:

// valid single emails: user@example.com
// valid single emails: "Display Name" <user@example.com>
// valid multiple emails: same as above but separated by , or ;
function isValidEmail($input, $allowMultiple = false) {

// Email formats described here: http://en.wikipedia.org/wiki/Email_address
// John Smith <john.smith@example.org>.
// "Display Name" <local-part@domain>

// parse out emails
$emails = array();
static $VALID_EMAIL_REGEXP = '(?:[\w\-]+[\w\-\.\+])*[\w\-]+@(?:[\w\-]+[\w\-.])*[\w\-]+\.[A-Za-z]{2,8}';
while (preg_match("/\A()($VALID_EMAIL_REGEXP)[,; ]*/", $input, $matches) ||
preg_match("/\A([^<]*) <($VALID_EMAIL_REGEXP)>[,; ]*/", $input, $matches)) {
list($matchedString, $displayName, $email) = $matches;
$input = substr_replace($input, '', 0, strlen($matchedString)); // remove matched content
$emails[] = $email;
}

// check for errors
if (!$emails) { return false; } // No valid emails or empty string is invalid
if ($input != '') { return false; } // remaining content means input has invalid email
if (!$allowMultiple && count($emails) > 1) { return false; }

//
return true;
}


Hope that helps!
Dave Edis - Senior Developer
interactivetools.com