Validate valid emails in a textarea

4

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>
    
asked by Hoose 22.03.2018 в 17:07
source

2 answers

2

An example with fewer lines using filters

$("#txtEmailAll").blur(function () {
    let e_stringCorreos = $(this).val()
                                 .replace(/\s+/g, '');
    const m_correos = e_stringCorreos.split(",");
    
    correos_validos = m_correos.filter(function(value){
      return /(.+)@(.+){2,}\.(.+){2,}/.test(value);
    })
    console.log(correos_validos.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>
    
answered by 22.03.2018 / 17:31
source
4

Only 2 dimensions to your code.

  • In the forEach: You already have the text, it is not necessary to look for it in the array and add to a variable.
  • Instead of deleting because you do not add the correct mails to an array?
  • Your code would look like this:

    $("#txtEmailAll").blur(function () {
        var e_stringCorreos = $(this).val();
        e_stringCorreos = e_stringCorreos.replace(/\s+/g, '');
        var m_correos = e_stringCorreos.split(",");
        var all_mails = [];
        m_correos.forEach(function (current, index) {
            if ( /(.+)@(.+){2,}\.(.+){2,}/.test(current) !== false ) {
                console.log('Correo válido: ' + current);
                all_mails.push(current);
            } else {
                console.log('Correo inválido: ' + current);
            }
        });
    
        var cleanEmails = all_mails.join(", ");
        $(this).val(cleanEmails);
        console.log('Correos válidos: ' + all_mails.join(", "));
    });
    <textarea id="txtEmailAll"></textarea>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        
    answered by 22.03.2018 в 17:27