1

I am trying to return a value from a javascript function that checks whether a string of multiple email addresses contains valid emails:

validateEmail function:

validateEmail = function(email) {
    var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    return re.test(email);
} // this returns true/false

var emails = ["[email protected]", "[email protected]"]

var emailResult = function(emails) {
        var result;
        emails.forEach(function(email) {
            if(validateEmail(email.trim()) === false) {
                result = false;
            }
        });
        return result
    }

If any of the emails are valid, I want emailResult to be equal to false. However, when I run console.log(emailResult), I just get a print out of my function's code. Can someone help?

Thanks!!

2
  • 2
    console.log(emailResult(emails))? (You also want to start with var result = true;) Commented Sep 9, 2015 at 1:13
  • emailResult is the function itself, if you want the return value, you have to call the function. emailResult([]) will return the initial result definition Commented Sep 9, 2015 at 1:24

2 Answers 2

2

You are sending the function itself to console.log(). You need to call the function, and pass the result of the function to console.log(), like so:

console.log(emailResult(emails));
Sign up to request clarification or add additional context in comments.

Comments

0

That's because you need to invoke the function. You could get it to work if you did console.log(validateEmail("test@"test.com")); instead or you can restructe like so:

function validateEmail (email) {
       var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
       return re.test(email);
} // this returns true/false

var emails = ["[email protected]", "[email protected]"]

var emailResult = function(emails) {
       var result;
       emails.forEach(function(email) {
              if(validateEmail(email.trim()) === false) {
                     result = false;
              }
       });
       return result
}

var theEmails = validateEmail("[email protected]");

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.