I have a textarea that validates a string of emails separated by commas, or better known as CSV. The event works half-heartedly, because it does what I want but not as I would like.
When adding a string, if there are 2 invalid emails followed and separated by a comma, ignore the second one and take it as a valid email. However, if I go back into the textarea and do the blur, that email erases it, something that is as it should be, but not the first blur.
As a note, this only happens to me when there are 2 invalid emails in a row.
Test chain
jijiji, jojojojo, [email protected], [email protected], jeje
$("#txtEmailAll").blur(function () {
var e_stringCorreos = $(this).val();
e_stringCorreos = e_stringCorreos.replace(/\s+/g, '');
var m_correos = e_stringCorreos.split(",");
m_correos.forEach(function (current, index) {
var e_correo = m_correos[index];
if ( /(.+)@(.+){2,}\.(.+){2,}/.test(e_correo) == false ) {
m_correos.splice(index, 1);
console.log('Correo inválido: ' + e_correo);
}
});
var cleanEmails = m_correos.join(", ");
$(this).val(cleanEmails);
console.log('Correos válidos: ' + m_correos.join(", "));
});
<!-- jijiji, jojojojo, [email protected], [email protected], jeje -->
<textarea id="txtEmailAll"></textarea>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>