2

I have an HTML form that i want to validate with a regex. I have the regex contained in a method called emailRegex, which is then called within the validateEmail method. I"m getting an error when it tries to call my emailRegex method within the validateEmail. Can anyone tell me what I'm doing wrong here?

var validation={

emailRegex: function() {//new email Regular Expression for validateEmail method
    return /^(([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+  ([;.](([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+)*$/;
},

validateEmail: function(value) {

    var regex = this.emailRegex;//takes value found in emailRegex

    var offensiveWords = new RegExp(/\b(hate|notCool)b/); //offensive words regular expression

    var email = document.getElementById("email");

    var compactEmail = function(){//takes the value of the email form element and takes out any white spaces
        var emailValue = email.value;
        var compacted = emailValue.replace(" ","");
        return compacted;
    }

    if(!regex.test(compactEmail())){//checks for a valid email address against regex from emailRegex method

        alert("Please enter a valid email address");
        email.focus();

        return false; 
    }

    if(!offensiveWords.test(compactEmail())){//checks for the offensive words found in offensiveWords regular expression.

        alert("Your email address contains an offensive word");
        email.focus();

        return false;
    }

    return true;

}
}
2

1 Answer 1

3

I'm guessing you are seeing a ReferenceError:

ReferenceError: emailRegex is not defined

Your variable, emailRegex is a function, not a property.

This is how you should call it instead:

var regex = this.emailRegex();
Sign up to request clarification or add additional context in comments.

1 Comment

I have another quick question. on my second regex, offensiveWords, am I using the correct syntax with that? I want to check the input against those two words: hate and notCool.

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.