Adrian Florescu
Adrian Florescu

Reputation: 4492

Validate multiple emails with JavaScript

I have these two functions:

validateEmail: function(value) {
    var regex = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
return (regex.test(value)) ? true : false;
}

validateEmails: function(string) {

    var self = shareEmail;
    var result = string.replace(/\s/g, "").split(/,|;/);

        for(var i = 0;i < result.length;i++) {
            if(!self.validateEmail(result[i])) {
                return false;
            } else {               
            return true;
        }
    }
}

The problem is that when I test the email like this if(!self.validateEmails(multipleEmails)) { i get true or false based only on the first email in the string, but I want to test for any email in the string.

Thank you!

Upvotes: 7

Views: 11143

Answers (5)

Qasim Bataineh
Qasim Bataineh

Reputation: 707

This code below is perfect to validate multiple email addresses separated with comma or semicolon using JQuery:

   var emailReg = new RegExp(/^([A-Z0-9.%+-]+@@[A-Z0-9.-]+.[A-Z]{2,6})*([,;][\s]*([A-Z0-9.%+-]+@@[A-Z0-9.-]+.[A-Z]{2,6}))*$/i);
      var emailText = $('#email').val();
      if (!emailReg.test(emailText)) {
          alert('Wrong Email Address\Addresses format! Please reEnter correct format');
            return false;
        }

Upvotes: 0

staabm
staabm

Reputation: 1575

in case you use jquery-validate the validation method would look like:

jQuery.validator.addMethod("separated_emails", function(value, element) {
    if (this.optional(element)) {
        return true;
    }

    var mails = value.split(/,|;/);
    for(var i = 0; i < mails.length; i++) {
        // taken from the jquery validation internals
        if (!/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test(mails[i])) {
            return false;
        }
    }

    return true;
}, "Please specify a email address or a comma separated list of addresses");

Upvotes: 2

Ghassan Elias
Ghassan Elias

Reputation: 2233

how about this?

validateEmails: function(string) {

    var self = shareEmail;
    var result = string.replace(/\s/g, "").split(/,|;/);
    var errors = [];
    for(var i = 0;i < result.length;i++) {
        if(!self.validateEmail(result[i])) {
            errors[i] = result[i] + ' is not valid.';
        }
     }
     if (errors.length > 0) {
        alert(errors.join('\n'));
        return false;
     } else {
         return true;
     }

}

Upvotes: 2

Anonymoose
Anonymoose

Reputation: 2461

I find the most maintainable way is to use a variable to store the return output.

validateEmail: function(value) {
    var regex = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
return (regex.test(value)) ? true : false;
}

validateEmails: function(string) {

var self = shareEmail;
var result = string.replace(/\s/g, "").split(/,|;/);
var allOk = true;


    for(var i = 0;i < result.length;i++) {
        if(!self.validateEmail(result[i])) {
            allOk = false;
        } 
    }

return allOk;
}

Upvotes: 1

Lucas Green
Lucas Green

Reputation: 3959

The problem is your if/else block; You are returning under both conditions. Which means that it leaves the function after evaluating only one element.

I've modified validateEmails to demonstrate what you probably want to do:

validateEmail: function(value) {
    var regex = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    return (regex.test(value)) ? true : false;
}

validateEmails: function(string) {
    var self = shareEmail;
    var result = string.replace(/\s/g, "").split(/,|;/);

    for(var i = 0;i < result.length;i++) {
        if(!self.validateEmail(result[i])) {
            return false;
        }
    }

    return true;
}

Upvotes: 12

Related Questions