简体   繁体   English

如何使用正则表达式允许多个 email id 用逗号分隔?

[英]How to allow multiple email id's separated by commas using regex?

Issue Description: - The below code works fine for single email id.问题描述: -以下代码适用于单个 email id。 However, when i pass multiple email id's each separated by commas then it returns false (alert("Please enter valid "+ labelInput +" mail id");).但是,当我传递多个 email id 时,每个都用逗号分隔,然后它返回 false(警报(“请输入有效的“+ labelInput +”邮件 id”);)。

I need to enable the user to allow multiple email id's comma separated.我需要让用户允许多个 email id 的逗号分隔。 Example: user1@xyz.com, user2@xyz.com, user3@zzz.com should return true.示例:user1@xyz.com、user2@xyz.com、user3@zzz.com 应该返回 true。

Note: - x[i] will contain the comma separated email id's.注意: - x[i] 将包含逗号分隔的 email id。

Code Snippet: -代码片段: -

var emailRGEX = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;           
            var emailResult = emailRGEX.test(x[i]);
            if (!emailResult) {
                alert("Please enter valid "+ labelInput +" mail id");
                $(this).focus();
                retFlag = false;
                return false;
}

Any help shall be greatly appreciated!任何帮助将不胜感激! Thanks!谢谢!

If the regex works fine, you should be able to surround the whole thing in a group with a comma at the end, then stick your existing regex at the end.如果正则表达式工作正常,您应该能够在最后用逗号将整个内容括在一个组中,然后将现有的正则表达式放在最后。 Ie ([existing regex here]\,)*([existing regex here])([existing regex here]\,)*([existing regex here])

In full, it would look like this:总的来说,它看起来像这样:

var emailRGEX = /^(\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+\,)*(\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+)$/;

You can split your emails and loop to check if email is valid or not.您可以拆分电子邮件并循环检查 email 是否有效。

 var emails = "123@gmail.com, 345hotmail.com"; emails = $.map(emails.split(","), $.trim); var emailRGEX = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/; for (var i = 0; i < emails.length; i++) { if (.emailRGEX;test(emails[i])) { alert("Please enter valid " + "message" + " mail id"). //$(this);focus(); //retFlag = false; //return false; } }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Note: Don't forget to trim to handle leading white space.注意:不要忘记修剪以处理前导空白。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用逗号分隔的多封电子邮件的正则表达式和有效的电子邮件 - Regex for multiple emails separated by commas with valid emails 用空格或逗号分隔的多个单词的正则表达式 - Regex for multiple words separated by spaces or commas rails使用正则表达式以逗号分隔的自动完成标记 - rails autocomplete tags separated by commas using regex 如何用正则表达式获取参数化用逗号分隔的字符串? - How to get parameterise a string separated by commas with regex? 如何使用 Jquery 或 javascript 添加多个 email id 验证,并用“;”分隔 “,”在文本区域 - How to add multiple email id validation using Jquery or javascript with separated by “;” “,” in textarea 有没有办法在 Jquery 中使用正则表达式在逗号分隔的数字中添加一个数字? - Is there a way to add a number in a number separated by commas using regex in Jquery? 正则表达式:允许多封电子邮件,以;分隔 (分号)并允许空白/空值 - Regex: Allow multiple emails separated by ; (semicolon) AND allow blank/empty value 用逗号分隔的javascript正则表达式验证列表 - javascript Regex verfiy list separated by commas 如何使用 parseFloat 允许逗号? - How to allow commas with parseFloat? 从复选框的数据 ID 中计算单个主题的值,该数据 ID 以逗号分隔的字符串格式 - count value of single subject from checkbox's data-id which is in string format separated by commas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM