-3

I need to validate a textbox that will accept a list of names and email addresses that are separated by commas. For an example check the picture below.

How I want to separate by name and emails: image

The code example is if I only separate by email address and I want to extend such a feature.

for (var i = 0; i < $scope.emailAddresses.length; i++) {
    var addr = $scope.emailAddresses[i];
    if (addr.address !== null && addr.address !== "") {
        message.users.push({email: addr.address});
    }
}
if($scope.massAddUsers !== undefined)
{
    var massEmails = $scope.massAddUsers.split(",");
    for(var j = 0; j < massEmails.length; j++)
    {
        var massEmailAddr = massEmails[j];
        if (massEmailAddr !== null && massEmailAddr.trim() !== "") {
            message.users.push({email: massEmailAddr.trim()});
        }
    }
}
Peter B
  • 22,460
  • 5
  • 32
  • 69

3 Answers3

1

As answered in this post you can validate email adresses by using

function validateEmail(email) {
    var re = /^(([^<>()\[\]\\.,;:\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 re.test(String(email).toLowerCase());
}

and use it like

var massEmails = $scope.massAddUsers.split(",");
for(massEmailAddr of massEmails ){
    massEmailAddr = massEmailAddr.trim();
    if(validateEmail(massEmailAddr)){
        message.users.push({email: massEmailAddr});
    }
    else if (massEmailAddr !== "") {
        message.users.push({name: massEmailAdd});
    }
}
Sebastian Speitel
  • 7,166
  • 2
  • 19
  • 38
0
  const list = input.split(",");
  const result = [];

  while(list.length > 2) {
    const [first, last, email] = list.splice(0, 3);
    // validate
    result.push({ first, last, email });
 }
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • How can I adjust my current email algorithm to include the names – Samuel Oyediran Mar 23 '18 at 10:25
  • How do I use splice to have the first , last and email in the correct order – Samuel Oyediran Mar 23 '18 at 13:17
  • While this code snippet may be the solution, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – yivi Mar 23 '18 at 13:35
  • @yivi i put the same effort into answering the OP put into asking. – Jonas Wilms Mar 23 '18 at 14:47
0

You should simply split the string first at every comma separated value, then loop through the array, up until there are only 3 elements left. Using splice, you can create a new array everytime and assign firstName, lastName and email to those array elements, because you know they belong together.

    // Get the string   
    var content = $scope.theUserInput;
    // Split string at every comma, which makes every comma seperated value an array element
    var array = content.split(',');
    // For every array element as long as there are at least 3 left
    while(array.length > 2) {
      // Take first 3 array element (e.g. felix, fritz, mail@a.com) and assign it to variables: first, last and email
      const [first, last, email] = array.splice(0,3);
      // Do your email verification here ...
      // then push to array all together:
      $scope.users.push({
        firstname: first,
        lastname: last,
        email: email
     });

A complete example: http://jsfiddle.net/U3pVM/42477/

ffritz
  • 2,180
  • 1
  • 28
  • 64