[英]Regex: Allow multiple emails separated by ; (semicolon) AND allow blank/empty value
Here is my regex: 这是我的正则表达式:
var emailsRegex = /^[\W]*([\w+\-.%]+@[\w\-.]+\.[A-Za-z]{2,4}[\W]*;{1}[\W]*)*([\w+\-.%]+@[\w\-.]+\.[A-Za-z]{2,4})[\W]*$/;
Currently it allows fully qualified single emails and multiple emails separated by semicolon, example: 当前,它允许完全合格的单个电子邮件和多个用分号分隔的电子邮件,例如:
email1@hi.com
email1@hi.com; email2@hi.com
email1@hi.com; email2@hi.com; email3@hi.com
...are all valid. ...都是有效的。
I want this to stay the same, but also allow blank/empty inputs. 我希望这保持不变,但也允许空白/空输入。 My form is flagging $invalid with a blank input field, even though the
required
attribute is not specified on the input field. 我的表单使用空白输入字段标记$ invalid,即使未在输入字段上指定
required
属性。
I suspect this is because it is not passing the regex validation. 我怀疑这是因为它没有通过正则表达式验证。 Thanks!
谢谢!
Please do not use a regex to match an email . 请不要使用正则表达式匹配电子邮件 。 First of all your regex is wrong (it won't match emails like
foo+bar@example.org
which is perfectly valid given RFC822 and newer RFCs). 首先,您的正则表达式是错误的(它与
foo+bar@example.org
这样的电子邮件不匹配,对于RFC822和较新的RFC,这是完全有效的)。 You should better use a library like verifyjs or fogcreek's email checker to check that email. 您最好使用诸如verifyjs或fogcreek的电子邮件检查器之类的库来检查该电子邮件。
Then all you have to do is to split your string around each emails using email_string.split(';')
and apply the checker on each of them. 然后,您要做的就是使用
email_string.split(';')
在每封电子邮件中拆分字符串,并在每封电子邮件上应用检查器。
HTH HTH
I ended up using string.split(;) and then passing through an improved RegEx which should account for 99% of email addresses in use today. 我最终使用了string.split(;),然后通过了经过改进的RegEx,它应该占今天使用的电子邮件地址的99%。 And I'm doing it inside an Angular Directive.
我正在Angular Directive中执行此操作。
It allows for empty inputs, multiple emails separated by ;
它允许空输入,多个电子邮件之间用
;
分隔;
which comply with the RFC for majority usage of email addresses. 符合RFC规定的电子邮件地址的大多数用法。
HTML HTML
<input type="text" id="emailCc" name="emailCc" ng-model="vm.ccRecipient" class="form-control input-sm" multiple-emails="vm.ccRecipient" placeholder="Email Cc" />
AngularJS AngularJS
angular.module('my-app')
.directive('multipleEmails', function () {
return {
require: 'ngModel',
link: function (scope, element, attrs, ctrl) {
ctrl.$parsers.unshift(function (rawInput) {
var emails = rawInput.split(';');
//console.log(emails);
// Consider not using complex regex validation for emails. See: https://davidcel.is/posts/stop-validating-email-addresses-with-regex/
// Instead, consider just checking for an "@" and a "." and call it a done. The mail daemon will return whether its a valid or invalid/bounced email address
//var emailsRegex = /.+@.+\..+/i;
// define single email validator here
var regexPattern = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
// angular.foreach(emails, function() {
var validityArr = emails.map(function (str) {
if (rawInput) {
return regexPattern.test(str.trim());
} else if (!rawInput) {
return true;
}
}); // 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 rawInput;
} else {
ctrl.$setValidity('multipleEmails', false);
return undefined;
}
});
}
};
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.