Validate multiple email id's in single line text field seperated by commas

AKASH BANERJEE
Mega Guru

We are using the below on change client script to validate multiple email address seperated by comma in single line text field name 'cc_email_addresses', But it is throwing error 'There is a javascript error in your browser console'. Please let me know if anyone having any idea if we have to make any changes in any part of the script.

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var err_field = control.id;

var err_flag = true;

var err_message = 'Invalid Email Address';


g_form.hideErrorBox(err_field);


var group_members = g_form.getValue('cc_email_addresses');


var member_split = group_members.split(',');


for (var n = 0; n < member_split.length; n++) {


var member_info = member_split[n].trim();


regex = /[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i;


err_flag = regex.test(member_info);


if (!err_flag) {


break;


}


}


if (!err_flag) {


g_form.hideFieldMsg('req_inbox');


g_form.showFieldMsg(err_field, err_message, 'error');


} else {


g_form.hideErrorBox(err_field);


}


}

4 REPLIES 4

Saurav11
Kilo Patron
Kilo Patron

Hello, is it catalog client script and error os on the portal of yes generally it happens when you use a syntax which is not aupported in the portal.

 

Bte in hour code what is the purpose of g_form.hideErrorBox(err_field) tey to remove it from the vide and then try can you check and let me know

 

Thanks

 

 

AKASH BANERJEE
Mega Guru

I removed g_form.hideErrorBox(err_field); but still the same error it seems. Please let me know if there is any other script or enhancement needs to be done in this script

 

Please use the below script:

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;
        }


    }
}

NagaPavanaM
Tera Contributor

You can use below script:

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;
        }


    }
}