You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何验证逗号分隔的姓名与邮箱列表文本框并扩展现有代码

Extending Comma-Separated List Handling to Support Names + Emails

Got it, let's fix this up. Your current code works great for plain comma-separated emails, but we need to adjust it to handle entries that include a name paired with an email—like John Doe <john@example.com> or Jane Smith, jane@example.com. Here's how to do it cleanly:

First, let's create a helper function that takes a single entry (from the split list) and parses out the name and email. This will handle both formatted name+email entries and plain email addresses:

function parseNameAndEmail(entry) {
  // Trim whitespace first to avoid issues with extra spaces
  const trimmedEntry = entry.trim();
  
  // Case 1: Entry is in "Name <email>" format
  const angleBracketMatch = trimmedEntry.match(/^(.+)\s+<([^>]+)>$/);
  if (angleBracketMatch) {
    return {
      name: angleBracketMatch[1].trim(),
      email: angleBracketMatch[2].trim()
    };
  }
  
  // Case 2: Entry is in "Name, email" format
  const commaSplit = trimmedEntry.split(/\s*,\s*/);
  if (commaSplit.length === 2) {
    return {
      name: commaSplit[0].trim(),
      email: commaSplit[1].trim()
    };
  }
  
  // Case 3: Just an email (fallback to original behavior)
  return {
    name: null, // or empty string if you prefer ""
    email: trimmedEntry
  };
}

Now, update your existing code to use this helper instead of just pushing the raw email. We'll adjust both the emailAddresses loop and the massAddUsers processing:

// Process individual email addresses (if these might have names too, adjust as needed)
for (var i = 0; i < $scope.emailAddresses.length; i++) {
  var addr = $scope.emailAddresses[i];
  if (addr.address !== null && addr.address !== "") {
    // If addr has a name property already, use it; else parse the address
    const user = addr.name ? 
      { name: addr.name, email: addr.address } : 
      parseNameAndEmail(addr.address);
    message.users.push(user);
  }
}

// Process the comma-separated mass input
if ($scope.massAddUsers !== undefined) {
  var massEntries = $scope.massAddUsers.split(/\s*,\s*/); // Split on commas with optional whitespace
  for (var j = 0; j < massEntries.length; j++) {
    var entry = massEntries[j];
    if (entry !== null && entry.trim() !== "") {
      const parsedUser = parseNameAndEmail(entry);
      // Only push if the email is valid (you might want to add email validation here too)
      if (parsedUser.email.trim()) {
        message.users.push(parsedUser);
      }
    }
  }
}

Key Improvements:

  • Flexible Parsing: Handles the two most common name+email formats, plus plain emails (so it's backward-compatible with your original code)
  • Whitespace Handling: Trims extra spaces everywhere to avoid invalid emails or messy names
  • Backward Compatibility: Still works with plain email lists like your original code did
  • Cleaner Code: The helper function keeps the main logic readable instead of cluttering the loops with parsing logic

Example Test Input:

If $scope.massAddUsers is set to:
John Doe <john@example.com>, Jane Smith, jane@example.com, bob@example.com

The parsed message.users will be:

[
  { name: "John Doe", email: "john@example.com" },
  { name: "Jane Smith", email: "jane@example.com" },
  { name: null, email: "bob@example.com" }
]

You might also want to add an email validation regex inside the parseNameAndEmail function to ensure the extracted email is valid—just add a check before returning the object if that's a requirement for your use case.

内容的提问来源于stack exchange,提问作者Samuel Oyediran

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:49:48