Multiple email population on single text field is not working

NagaPavanaM
Tera Contributor
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    if (oldValue !== newValue) {

        var emails = newValue.split(/\s*[;,]\s*|\s+/);
        var validDomains = ['@manulifeam.com', '@manulife.com', '@jhancock.com'];
        var emailRegex = /^(?![\d])[a-zA-Z0-9!#$%*\/?\^{}~&'\+\-=_.]+(?<![.])@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
        var allValid = true;

        for (var i = 0; i < emails.length; i++) {
            var email = emails[i].trim(); // Trim spaces from each email

            if (email) { // Make sure the email is not an empty string
                // Validate email format
                var isValidFormat = emailRegex.test(email);

                // Check if the email ends with one of the valid domains
                var valid = false;
                for (var j = 0; j < validDomains.length; j++) {
                    if (email.endsWith(validDomains[j])) {
                        valid = true;
                        break;
                    }
                }

                // Email is valid if it has the correct format and domain
                if (isValidFormat && valid) {
                    formattedEmails.push(email);
                } else {
                    allValid = false;
                }
            }
        }

        if (!allValid) {
            g_form.clearValue('group_distribution_list');
            g_form.addErrorMessage("One or more email addresses are invalid or do not match the required domains.");
            return;
        }


    }
}
0 REPLIES 0