有没有办法验证单个输入字段中的一个或多个电子邮件地址?
我目前正在探索的方式是创建一个自定义指令,如果检测到逗号,将会将输入拆分成不同的电子邮件.这是我到目前为止
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. }); } }; });
编辑:plunkr
编辑2:结果我可以使用角度1.3
http://plnkr.co/edit/YrtXOphxkczi6cwFjvUp?p=preview
.directive('multipleEmails',function () { return { require: 'ngModel',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,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; } // }) }); } }; });