最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript validate list of email addresses comma separated - Stack Overflow

programmeradmin3浏览0评论

I am using ServiceNow and I need to validate a textbox that will accept a list of email addresses that are separated by mas.

The list of email addresses is in the variable group_members.

The validation works for the first email address only. The second email address in the list is showing as invalid even though I have tested with a list of valid addresses. For example:

[email protected], [email protected]

testa will show up as valid but testb will be invalid. I know it is not the regex because the same email address will return valid as long as it is the first in the list. I think there is something wrong with looping over the array or perhaps the way that the ma separated list is being split to the array.
Can you check my logic here to see what is wrong?

var group_members = g_form.getValue('u_group_members');
var member_split = group_members.split(',');

for (var n = 0; n < member_split.length; n++) {
  var member_info = member_split[n];
  var validRegExp = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;

    if (member_info.search(validRegExp) == -1) {
        g_form.showFieldMsg('u_group_members', "Group Members contains an invalid email address. " , 'error');
    }
    else if (member_info.search(validRegExp) == 0) {
        g_form.showFieldMsg('u_group_members', "Group Members email addresses are valid");
    }
}

I am using ServiceNow and I need to validate a textbox that will accept a list of email addresses that are separated by mas.

The list of email addresses is in the variable group_members.

The validation works for the first email address only. The second email address in the list is showing as invalid even though I have tested with a list of valid addresses. For example:

[email protected], [email protected]

testa will show up as valid but testb will be invalid. I know it is not the regex because the same email address will return valid as long as it is the first in the list. I think there is something wrong with looping over the array or perhaps the way that the ma separated list is being split to the array.
Can you check my logic here to see what is wrong?

var group_members = g_form.getValue('u_group_members');
var member_split = group_members.split(',');

for (var n = 0; n < member_split.length; n++) {
  var member_info = member_split[n];
  var validRegExp = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;

    if (member_info.search(validRegExp) == -1) {
        g_form.showFieldMsg('u_group_members', "Group Members contains an invalid email address. " , 'error');
    }
    else if (member_info.search(validRegExp) == 0) {
        g_form.showFieldMsg('u_group_members', "Group Members email addresses are valid");
    }
}
Share Improve this question edited Jun 10, 2022 at 15:33 Chenmunka 8117 gold badges29 silver badges42 bronze badges asked May 4, 2015 at 22:00 pengzpengz 2,4717 gold badges55 silver badges96 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

It's because you have a space after the ma. Either remove it or account for it in your regex.

var group_members = "[email protected], [email protected]";

document.getElementById('space').addEventListener('click', function(){
  document.getElementById('el').innerHTML = "";
  run();
});

document.getElementById('trim').addEventListener('click', function(){
  document.getElementById('el').innerHTML = "";
  run(true);
});

function run(trim) {
  var member_split = group_members.split(',');

  var valid = true;
  for (var n = 0; n < member_split.length; n++) {
    var member_info = trim ? member_split[n].trim() : member_split[n];
    var validRegExp = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;

    if (member_info.search(validRegExp) === -1) {
        valid = false;
        break;
    }
  }
  if (valid)
    document.getElementById("el").innerHTML += "Group Members email addresses are valid.<br/>";
  else
    document.getElementById("el").innerHTML += "Group Members contains an invalid email address.<br/>";
}
<button id="space">Space</button><button id="trim">Trim it</button>
<div id="el"><div>

发布评论

评论列表(0)

  1. 暂无评论