0

When I write the following in Webstorm, it does not work. I get "function name expected." Any help is greatly appreciated. I'm using the book "The Principles of Object-Oriented Javascript," page 81 as my guide.

var validator = (function ()
{
    //source: https://www.w3resource.com/javascript/form/email-validation.php
    var EMAIL_PATTERN = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;

    return
    {
        validateEmail: function(email)
        {
            return EMAIL_PATTERN.test(email);
        }
    };
}());

console.log(validator.validateEmail("whatever@somewhere.com"));

1 Answers1

0

It looks to be just an issue with your return statement

var validator = (function ()
{
    //source: https://www.w3resource.com/javascript/form/email-validation.php
    var EMAIL_PATTERN = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;

    return {
        validateEmail: function(email)
        {
            return EMAIL_PATTERN.test(email);
        }
    };
}());

console.log(validator.validateEmail("whatever@somewhere.com"));

is a corrected version.

EliKor
  • 199
  • 3
  • Thanks! I upvoted your answer. Do you think this is due to javascript grammar or webstorm syntax checking? –  Nov 20 '20 at 04:06
  • It is due to javascript's interpreter. See this [post](https://stackoverflow.com/questions/8528557/why-doesnt-a-javascript-return-statement-work-when-the-return-value-is-on-a-new) for more details – EliKor Nov 20 '20 at 04:19
  • Good ole' Javascript. lol. Thank both of you for the links. –  Nov 20 '20 at 04:22