1

I have script in JS:

$("#mailstextarea").on("change keyup paste click", function(){
    var text = this.value;
    var rez =  text.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
    document.getElementById("emails").innerHTML = rez;

    for(var i = 0; i < rez.length; i++){
        addRow(rez[i]);
    }

})

When in #mailstextarea I type text with mails, I see this mails in div #emails, but I get error on rez.length „Cannot read property 'length' of null”. Why? Thanks!

1

1 Answer 1

1

.match() will return null if nothing is matched.

Just add a if check:

if (rez) {
    for(var i = 0; i < rez.length; i++){
        addRow(rez[i]);
    }
}

Or you could set rez to an empty array when nothing matched.

var rez =  text.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi) || [];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.