1

I want to check a comma separated email addresses with regex in Laravel 5.1 validation and I have created below regex for email but it is not working for me.

regex: /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i

I want to allow email like this: test1test.com, test2@test.com, test3@test.com

Here is my validation example code:

$rules = array(
    'email' => 'regex:/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i',
);
$messages = [
    'email.regex' => 'Please enter valid Email.',
];

$validator = Validator::make(Input::all(), $rules, $messages);

if ($validator->fails()) {

} else {

}
Mr.Happy
  • 2,639
  • 9
  • 40
  • 73
  • See http://stackoverflow.com/questions/4412725/how-to-match-a-comma-separated-list-of-emails-with-regex. You may either use that approach (`^(` + your regex + `,?)+$`) or use something more PCRE-like `/^([_a-z0-9-]+(?:\.[_a-z0-9-]+)*@[a-z0-9-]+(?:\.[a-z0-9-]+)*\.[a-z]{2,3})(?:,(?1))*$/i` – Wiktor Stribiżew Jan 12 '17 at 09:06
  • Email isn't quite that simple to validate: https://hexillion.com/samples/ < scroll down to the Regular Expressions section. – CD001 Jan 12 '17 at 09:08
  • Comma's are not allowed in e-mail addresses, so you could also explode on the comma, trim the results and use `filter_var()` to check each entry individually. – jeroen Jan 12 '17 at 09:08
  • why not explode the string by comma and validate each element via built-in laravel email validator rule? – Alex Andrei Jan 12 '17 at 09:08
  • isn't it an option to use explode and check them in foreach? – Paul Roefs Jan 12 '17 at 09:09
  • @AlexAndrei I don't know how to do that in Laravel. – Mr.Happy Jan 12 '17 at 09:09

5 Answers5

5

You can try this

foreach(explode(',', $sEmailAddresses) AS $sEmailAddress) {
$bValid |= filter_var($sEmailAddress, FILTER_VALIDATE_EMAIL);
 }

 $sResult = ($bValid) ? 'Both are valid' : 'One of them are not';
Indresh Tayal
  • 276
  • 1
  • 11
0

This is java-script validation

 var emails='test1test.com,test2@test.com,test3@test.com'; 
    var regex = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
     var result = emails.replace(/\s/g, "").split(",");  
                    for(var i = 0;i < result.length;i++) {
                        if(!regex.test(result[i])) {
                            return false;
                        }
                    }      
Yogesh Singasane
  • 283
  • 3
  • 12
0

You can do it with replace() method of Request class to turn input into an array containing email addresses then do an array validation on it:

// Split form input value on commas followed by any number of spaces (returns an array)
$request->replace(array('email' => preg_split('~, *~', $request->email)));
// How we should define our validator rule
$validator = Validator::make($request->all(), [
    'email.*' => 'email',
]);
// Turn back to our first value
$request->email = implode(', ', $request->email);
revo
  • 47,783
  • 14
  • 74
  • 117
0

Try this as an alternative,

/^([_a-z0-9\-]+)(\.[_a-z0-9\-]+)*@([a-z0-9\-]{2,}\.)*([a-z]{2,4})(,([_a-z0-9\-]+)(\.[_a-z0-9\-]+)*@([a-z0-9\-]{2,}\.)*([a-z]{2,4}))*$/

To verify your email regex.

0

In reply to @Alex Andrei's comment:

Below you will find an example of the function I used to get the results you're looking for using PHP explode() to separate the emails. Obviously in order to get the exact functionality that you need, you should really dig into that documentation :)

public function validateEmail()
    {
        $emails = "john@email.com,jack@email.com,notanemail,jill@email.com";
        $explodedEmails = explode(",", $emails);


        foreach ($explodedEmails as $email)
        {
            $validator = \Validator::make(
                array('individualEmail' => $email),
                array('individualEmail' => 'email') 
            );

            if ($validator->passes())
            {
                echo "Email correct <br />";
            }
            elseif($validator->fails())
            {
                echo "Email not correct <br />";

            }

        };

    }

This echoes:

Email correct 
Email correct 
Email not correct 
Email correct 
Vixter
  • 1
  • 1