0

I am using this regular expression for validating one email.

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

How to validate multiple email addresses in a text box or text area with comma (,) and semicolon (;) separated at a time using PHP.

example: abac@gmail.com,abcd@gmail.com;abcede@gmail.com,acf@in.com

hakre
  • 193,403
  • 52
  • 435
  • 836
vvr
  • 456
  • 3
  • 17
  • 30
  • i think first you need to extract all the email patterns from the text and then validate them. – Arfeen Dec 23 '11 at 07:40
  • 4
    If available, use [`mailparse_rfc822_parse_addresses()`](http://php.net/mailparse_rfc822_parse_addresses). Else split up the list on commas, trim, and validate each list entry (either with your non-compliant regex, or `filter_var(.., FILTER_VALIDATE_EMAIL)`). – mario Dec 23 '11 at 07:42
  • 1
    So… reject email addresses that include a `+` in them, and any from the `.info` or `.museum` TLDs. This regex is even worse then most that try to check email addresses. – Quentin Dec 23 '11 at 08:52

2 Answers2

4
$emails = preg_split('[,|;]',$_POST['emails']);
foreach($emails as $e){
    if(preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/',trim($e)) == 0){
        echo($e ."is not a valid email address");
    }
}

Should break your incoming emails into an array of emails. Then will go through each email and check the regular expression. Will output that an email is invalid as it finds them. Feel free to replace the echo with whatever you want the code to do if an email is invalid. Edited: breaks both commas and semicolons

Edit:Regular expression was changed. (Sorry I hadn't checked it before posting.)

David Richard
  • 356
  • 1
  • 7
  • I want both comma and semicolon separated values to be validate. This will help me to do for only one separated value. – vvr Dec 23 '11 at 08:36
  • Sorry, misunderstood the question. Alright, now it should do both commas and semicolons. – David Richard Dec 23 '11 at 08:49
  • I am getting warnings when i tried like this Warning: preg_split() [function.preg-split]: Delimiter must not be alphanumeric or backslash Warning: Invalid argument supplied for foreach() – vvr Dec 23 '11 at 09:16
2

Okay, here is an howto on how to transform your regex into matching a list of something: validate comma separated list using regex

The simple approach is splitting up your string, and then validating each entry (either is inexact, as commas are actually allowed in addresses):

 $list = str_getcsv($emails);   // or preg_split on commas
 foreach ($list as $mail) {
     if (!filter_var($mail, FILTER_VALIDATE_EMAIL)) {
         return false;
     }
 }

Or you use the Using a regular expression to validate an email address and exchange the last line (?&address)/x for:

 ^\s* (?&address) (\s*,\s* (?&address) )* \s* $ /x
Community
  • 1
  • 1
mario
  • 144,265
  • 20
  • 237
  • 291
  • I want both comma and semicolon separated values to be validate. This will help me to do for only one separated value. – vvr Dec 23 '11 at 08:49
  • 1
    You can just use `[,;]` for the regex or with a custom `preg_split`. – mario Dec 23 '11 at 08:58