`I am working on an ASP.NET Web Forms application where users need to enter multiple email addresses into a TextBox. Sometimes, users enter email addresses without any separators (e.g., binu.combinu.ae), and I need to automatically separate these with semicolons (;) when the input field loses focus.
I’ve implemented a basic solution using JavaScript to handle the blur event, but I’m facing issues when trying to intelligently split email addresses based on potential separators like “@” or “.”.
>
> “`
`
Email Separator
function formatEmails() {
const emailInput = document.getElementById(‘<%= emailInput.ClientID %>’); // Replace with your input ID
let emailString = emailInput.value.trim();const emailRegex = /([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,})/g;
let result = ”;
let match;
let lastIndex = 0;your text
while ((match = emailRegex.exec(emailString)) !== null) {
Add the matched email to the result
result += match[0] + ‘;’;
lastIndex = emailRegex.lastIndex;}
Handle any remaining text that could be a partial email
if (lastIndex < emailString.length) {
result += emailString.substring(lastIndex).replace(/;/g, ”) + ‘;’;}
emailInput.value = result;
}
document.addEventListener(‘DOMContentLoaded’, () => {
const emailInput = document.getElementById(‘<%= emailInput.ClientID %>’);
emailInput.addEventListener(‘blur’, formatEmails); (unfocus)}); </script>
`
`
““`
How can I improve my JavaScript function to reliably separate concatenated email addresses with semicolons in an ASP.NET Web Forms application? Are there any best practices or more robust regex patterns I should consider for this task?`