I have a textarea where user can enter multiple email address. So is there any way to validate them all.
Currently I am using ng-pattern to vaalidate it.
I have a textarea where user can enter multiple email address. So is there any way to validate them all.
Currently I am using ng-pattern to vaalidate it.
Found this one and did the job for me
.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.
var re = /\S+@\S+\.\S+/;
// angular.foreach(emails, function() {
var validityArr = emails.map(function(str){
return re.test(str.trim());
}); // sample return is [true, true, true, false, false, false]
console.log(emails, validityArr);
var atLeastOneInvalid = false;
angular.forEach(validityArr, function(value) {
if(value === false)
atLeastOneInvalid = true;
});
if(!atLeastOneInvalid) {
// ^ all I need is to call the angular email checker here, I think.
ctrl.$setValidity('multipleEmails', true);
return viewValue;
} else {
ctrl.$setValidity('multipleEmails', false);
return undefined;
}
// })
});
}
};
});
see here