[英]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.