的Javascript多个电子邮件正则表达式验证
问题描述:
通常简单的电子邮件的验证是:的Javascript多个电子邮件正则表达式验证
/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/
这将验证像[email protected]
但如何验证,如果邮件是多个电子邮件?
entry 1: [email protected], [email protected], [email protected]
entry 2: [email protected] , [email protected] , [email protected]
entry 3: [email protected], [email protected] , [email protected]
entry 4: [email protected]
此电子邮件是用户可能输入的条目。另外预计他们有时会收到2或3或4或更多的电子邮件。
感谢您的答案。
答
分割上一个逗号的电子邮件和验证项
var x = getEmails();
var emails = x.split(",");
emails.forEach(function (email) {
validate(email.trim());
});
凡getEmails()获取网页的电子邮件,并验证运行你对电子邮件的正则表达式
答
你应该能够分裂通过逗号输入,然后针对正则表达式测试各个电子邮件子条目。
var valid = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var entries = entry.split(",");
if(valid.test(entries[0]))... //or however your testing against the regex
您可能还想在测试任何剥离的电子邮件子字符串之前修剪任何空格。
答
尝试此jQuery验证:
jQuery.validator.addMethod("multiemail", function (value, element) {
if (this.optional(element)) {
return true;
}
var emails = value.split(','),
valid = true;
for (var i = 0, limit = emails.length; i < limit; i++) {
value = emails[i];
valid = valid && jQuery.validator.methods.email.call(this, value, element);
}
return valid;
}, "Invalid email format: please use a comma to separate multiple email addresses.");
答
试试这个
function validateEmails(string) {
var regex = /^([\w-\.][email protected]([\w-]+\.)+[\w-]{2,4})?$/;
var result = string.replace(/\s/g, "").split(/,|;/);
for(var i = 0;i < result.length;i++) {
if(!regex.test(result[i])) {
return false;
}
}
return true;
}
接受逗号和分号作为分隔
答
function validateEmail(emailAddress) {
var emailPattern = /^(([^<>()[\]\\.,;:\[email protected]\"]+(\.[^<>()[\]\\.,;:\[email protected]\"]+)*)|(\".+\"))@((\[[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,}))$/;
return emailPattern.test(emailAddress);
}
function validate() {
$("#result").text("");
var email = $("#emailAddress").val();
if (validateEmail(email)) {
$("#result").text(email + " validation successful");
$("#result").css("color", "white");
} else {
$("#result").text(email + " validation failed");
$("#result").css("color", "red");
}
return false;
}
$("form").bind("submit", validate);
.divSection{
text-align: center; padding: 8%;
}
.pSection{
border: none; color: white; padding: 10px 100px; text-align: center; text-decoration: none; display: inline-block; font-size: 24px; margin: 3% 0%; border-radius: 6px; -webkit-transition-duration: 0.4s; transition-duration: 0.4s; font-family: Roboto-Regular,Helvetica,Arial,sans-serif; background-color: #4184f3; margin: auto;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="divSection">
<form action="dashboard/user/profile" method="POST">
<input id="emailAddress" placeholder="Enter Email" value="[email protected]">
<input type='submit' value="check">
</form>
</div>
<div class="divSection" >
<p class="pSection" id='result'></p>
</div>
这是JavaScript代码来检查,如果邮件中包含一个以上的@字符
var count=0;
email = "[email protected]@gmail.com";
alert(email);
for(i =0; i<email.length;i++){
if(email.charAt(i) == "@"){
count++;
}}
if(count>1){
alert("not ok")
} else {
alert("ok")
}
另一种方法是通过使用电子邮件的标准模式
var pattern= /^(([^<>()\[\]\\.,;:\[email protected]"]+(\.[^<>()\[\]\\.,;:\[email protected]"]+)*)|(".+"))@((\[[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,}))$/;
re.test(email);