我有一个textarea,用户可以输入多个电子邮件地址。那么有没有办法验证它们。
目前我正在使用ng-pattern对其进行删除。
答案 0 :(得分:0)
找到这个,为我做了工作
.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;
}
// })
});
}
};
});
请参阅here