I have a textarea that validates a string of emails separated by commas, or better known as CSV. The event works halfway, because it does what I want but not how I would like it to.
When adding a string, if there are 2 invalid emails in a row and separated by commas, it ignores the second one and takes it as a valid email. However, if I go back into the textarea and do the blur, that mail deletes it, which is how 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>
Only 2 annotations to your code.
Your code would look like this:
An example with fewer lines using filters