Validate 2 or more emails in one input

1

I have a registration that does email validation in the focusout event in Input. This validation works perfectly. Now there is a need to validate multiple emails within the same Input, separating by ";" and how could you handle each email at once?

$("#cobrancaEmail").focusout(function () {
    var valor = $("#cobrancaEmail").val();
    //valida se o email é válido
    if (validaEmailIE(valor)) {
        $("#cobrancaEmail").css({ "border-color": "blue", "padding": "1px" });
        $("#errocobrancaEmail").html("");
    } else {
        $("#cobrancaEmail").css({ "border-color": "red", "padding": "1px" });
        $("#errocobrancaEmail").html("E-mail inválido!");
    }
});


function validaEmailIE(email) {
    var regex = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    return regex.test(email);
}
    
asked by anonymous 20.07.2018 / 12:27

1 answer

4

Within the function of focusout you could add split and forEach :

var valor = "[email protected];[email protected];[email protected]"
var contador = 0;

var arr = valor.split(';')

arr.forEach( function (element, index, array) {
    if (!validaEmailIE(element))
        contador++
})

if (contador > 0) {
    // algum email é inválido
} else {
    // todos os email são validos
}

References: Split , forEach

    
20.07.2018 / 12:57