Email validation client script (easy points)

juan casas
Mega Expert

Hi experts

 

I have a onSubmit client script that is failing .....

my task is to verify the email format being inputted.

 

So for some weird reason, the code works for evaluating 1 email, and it also works in Xplore.

However, when the user inputs more than one email, the code does not work and I get a weird error message

onSubmit script error: TypeError: emailToValidate.match is not a function:
function () { [native code] }

 

So this is how the code works,

check if there are multiple emails by looking for a comma in the field value "cc",

if there arent' evaluated the email using the function validateEmail function

if there are multiple emails, evaluate each email using  validateEmail function

 

 

 





function onSubmit() {
    function validateEmail(emailToValidate) {
        var validRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
        if (emailToValidate.match(validRegex) && emailToValidate.split('@')[1].indexOf('.') != -1) {
            return true;
        } else {
            g_form.showFieldMsg('cc', 'Incorrect format');
            return false;
        }

    }

    var emailToValidate = g_form.getValue('cc');
    var isEmailValid = true;
    // verify if multiple emails present
    if (emailToValidate.indexOf(',') != -1) {
        // there are multiple emails present
        var arrEmails = emailToValidate.split(',');
        for (var i in arrEmails) {
            isEmailValid = validateEmail(arrEmails[i]);
            if (!isEmailValid) break;
        }
    } else {
        isEmailValid = validateEmail(emailToValidate);
    }
    return isEmailValid;

}