Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate multiple email addresses in single field

Is there a way to validate one or multiple email addresses in a single input field?

The way that I'm currently exploring is by creating a custom directive that will split the input into different emails if a comma is detected. This is what I have so far:

angular.module('myApp')
  .directive('multipleEmails', function () {
    return {
      require: 'ngModel',
      link: function(scope, element, attrs, ctrl) {
        ctrl.$parsers.unshift(function(viewValue) {

          var emails = viewValue.split(',');
          // loop that checks every email, returns undefined if one of them fails.
        });
      }
    };
  });

The issue that I have with this is that I have been unable to call the angular email validator manually.

Edit: plunkr

Edit 2: Turns out that I can use angular 1.3

like image 828
alexdmejias Avatar asked Dec 01 '14 17:12

alexdmejias


People also ask

How do I verify multiple email addresses?

You just parse the string into an array of string, each with a single email address. Split the string on your delimiter, get the array of string back and then enumerate the array, sending each email address to your code that validates the address.

What is email validation in JavaScript?

Email validation is one of the vital parts of authenticating an HTML form. Email is a subset or a string of ASCII characters, divided into two parts using @ symbol.


2 Answers

Instead of relying on the angular validator, I just ended up using a different regex. This is what I ended up with, which is very similar to what is shown here :

angular.module('myApp')
  .directive('multipleEmails', function () {
    return {
      require: 'ngModel',
      link: function(scope, element, attrs, ctrl ) {
        var emailsRegex = /^[\W]*([\w+\-.%]+@[\w\-.]+\.[A-Za-z]{2,4}[\W]*,{1}[\W]*)*([\w+\-.%]+@[\w\-.]+\.[A-Za-z]{2,4})[\W]*$/;
        ctrl.$parsers.unshift(function(viewValue) {
          if (emailsRegex.test(viewValue)) {
            ctrl.$setValidity('multipleEmails', true);
            return viewValue;
          } else {
            ctrl.$setValidity('multipleEmails', false);
            return undefined;
          }
        });
      }
    };
  });
like image 83
alexdmejias Avatar answered Oct 18 '22 22:10

alexdmejias


I know it's a late answer, but uses the Angular 1.x logic as much as possible and only adds the multiple support. Other answers all use their own validation regex.

<input type="email" data-ng-multiple-email data-ng-model="$ctrl.x">

angular.module("myApp")
    .directive("ngMultipleEmail", function () {
        return {
            restrict: "A",
            require: "?ngModel",
            link: function (scope, elm, attrs, ctrl) {
                if (ctrl && ctrl.$validators.email) {
                    let originalValidator = ctrl.$validators.email;
                    ctrl.$validators.email = function (modelValue,   viewValue) {
                        var value = modelValue || viewValue || "",
                            valueSplit = value.split(/[,;]+/);
                        valueSplit.forEach(function (email) {
                            var valid = originalValidator(email.trim());
                            if (!valid) {
                                return false;
                            }
                        });
                        return true;
                    };
                }
            }
        };
    });
like image 42
Niels Steenbeek Avatar answered Oct 18 '22 23:10

Niels Steenbeek