Javascript 多电子邮件正则表达式验证

发布于 2024-12-17 02:27:24 字数 1757 浏览 3 评论 0原文

通常简单电子邮件的验证是:

/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/

这将验证电子邮件,例如 [email protected]< /a>

但是如何验证电子邮件是否是多个?

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 封或更多电子邮件。

感谢您的回答。

Normally validation of simple email is:

/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/

This will validate email like [email protected]

But how to validate if email is multiple?

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]

This emails is a possible entries that user will input. Also expect thier is 2 or 3 or 4 or more emails sometimes.

Thanks for the answers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

时间海 2024-12-24 02:27:24

用逗号分割电子邮件并验证条目

var x = getEmails();
var emails = x.split(",");
emails.forEach(function (email) {
  validate(email.trim());
});

getEmails() 从页面获取电子邮件,并验证针对电子邮件运行正则表达式

Split the emails on a comma and validate the entries

var x = getEmails();
var emails = x.split(",");
emails.forEach(function (email) {
  validate(email.trim());
});

Where getEmails() gets the emails from the page, and validate runs your regex against the emails

小忆控 2024-12-24 02:27:24

尝试

    function validateEmails(string) {
        var regex = /^([\w-\.]+@([\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;
    }

接受逗号和分号作为分隔符

try this

    function validateEmails(string) {
        var regex = /^([\w-\.]+@([\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;
    }

accept both comma and semicolon as separator

愿与i 2024-12-24 02:27:24

这是一个用于多个邮件验证的正则表达式,无需拆分

function validateMultipleEmails(string) {
    var regex = /^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-\.]+)+([;]([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-\.]+))*$/;
    return regex.test(string);
}

https://regex101.com/r/TUXLED/1< /a>

Here is a regex for multiple mail validation without split

function validateMultipleEmails(string) {
    var regex = /^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-\.]+)+([;]([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-\.]+))*$/;
    return regex.test(string);
}

https://regex101.com/r/TUXLED/1

脸赞 2024-12-24 02:27:24

您应该能够用逗号分隔条目,然后根据正则表达式测试各个电子邮件子条目。

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

在测试任何已剥离的电子邮件子字符串之前,您可能还需要修剪所有空格。

You should be able to split the entry by commas, and then test the individual email subentries against the regexp.

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

You might also want to trim any whitespace before testing any of the stripped email substrings.

墨落画卷 2024-12-24 02:27:24

尝试使用 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.");

Try this for jquery validation:

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.");
流殇 2024-12-24 02:27:24
function validateEmail(emailAddress) {
  var emailPattern = /^(([^<>()[\]\\.,;:\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,}))$/;
  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 = "shubham20.ye@[email protected]";
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= /^(([^<>()\[\]\\.,;:\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,}))$/;
re.test(email);

function validateEmail(emailAddress) {
  var emailPattern = /^(([^<>()[\]\\.,;:\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,}))$/;
  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>

This is javascript code to check if email contains more than one @ characters

var count=0;
email = "shubham20.ye@[email protected]";
alert(email);
for(i =0; i<email.length;i++){
if(email.charAt(i) == "@"){
count++;
}}
if(count>1){
   alert("not ok")
} else {
    alert("ok")
}

Alternate way is by using the standard pattern of the email

var pattern= /^(([^<>()\[\]\\.,;:\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,}))$/;
re.test(email);
栩栩如生 2024-12-24 02:27:24

ruby 动态数据中的 Javascript 多电子邮件正则表达式验证

  <span id="error_message_<%= item.id %>" class="p-3 mb-2 text-danger text-center">Multiple emails must be separated by comma.</span>
  <%= text_field_tag :email, nil, style: 'width: 60%;', required: true, id: "emails_#{item.id}", placeholder: "Multiple emails must be separated by comma." %>
  $(document).ready(function() {

    document.getElementById("cc_submit_<%= item.id %>").disabled = true;

    document.getElementById('error_message_<%= item.id %>').style.display='none';

  });

  $('#emails_<%= item.id %>').keyup( function() {

    var emails = $('#emails_<%= item.id %>').val();
    var emails = emails.split(",");
    var valid = true;
    var regex = /^(([^<>()[\]\\.,;:\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,}))$/;
    var invalidEmails = [];
    for (var i = 0; i < emails.length; i++) {
      emails[i] = emails[i].trim();
      if( emails[i] == "" || ! regex.test(emails[i])){
        invalidEmails.push(emails[i]);
      }
    }

    if(invalidEmails != 0) {

      document.getElementById("cc_submit_<%= item.id %>").disabled = true;

      document.getElementById("error_message_<%= item.id %>").style.display='block';

      }
    else{

      document.getElementById("cc_submit_<%= item.id %>").disabled = false;
      document.getElementById('error_message_<%= item.id %>').style.display='none';

    }
  })

Javascript multiple email regexp validation in ruby dynamic data

  <span id="error_message_<%= item.id %>" class="p-3 mb-2 text-danger text-center">Multiple emails must be separated by comma.</span>
  <%= text_field_tag :email, nil, style: 'width: 60%;', required: true, id: "emails_#{item.id}", placeholder: "Multiple emails must be separated by comma." %>
  $(document).ready(function() {

    document.getElementById("cc_submit_<%= item.id %>").disabled = true;

    document.getElementById('error_message_<%= item.id %>').style.display='none';

  });

  $('#emails_<%= item.id %>').keyup( function() {

    var emails = $('#emails_<%= item.id %>').val();
    var emails = emails.split(",");
    var valid = true;
    var regex = /^(([^<>()[\]\\.,;:\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,}))$/;
    var invalidEmails = [];
    for (var i = 0; i < emails.length; i++) {
      emails[i] = emails[i].trim();
      if( emails[i] == "" || ! regex.test(emails[i])){
        invalidEmails.push(emails[i]);
      }
    }

    if(invalidEmails != 0) {

      document.getElementById("cc_submit_<%= item.id %>").disabled = true;

      document.getElementById("error_message_<%= item.id %>").style.display='block';

      }
    else{

      document.getElementById("cc_submit_<%= item.id %>").disabled = false;
      document.getElementById('error_message_<%= item.id %>').style.display='none';

    }
  })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文