2

正規表現で検証したい HTML フォームがあります。私は、emailRegex というメソッドに含まれる正規表現を持っています。これは、validateEmail メソッド内で呼び出されます。validateEmail 内で emailRegex メソッドを呼び出そうとするとエラーが発生します。ここで何が間違っているのか教えてもらえますか?

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;

}
}
4

1 に答える 1