2013-07-30 4 views
2

정규식으로 유효성을 검사하려는 HTML 양식이 있습니다. regex는 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; 

} 
} 
+1

그 때 얻는 오류는 무엇입니까? –

+0

정규식을 다음과 비교하십시오. http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html –

답변

3

나는 당신이 ReferenceError보고있는 것 같은데요 :.?

ReferenceError: emailRegex is not defined

귀하의 변수, emailRegex 함수가 아닌 속성입니다

이 대신 호출하는 방법이다.

var regex = this.emailRegex(); 
+0

다른 빠른 질문이 있습니다. 내 두 번째 정규식, offensiveWords에서 올바른 구문을 사용하고 있습니까? hate와 notCool이라는 두 단어에 대한 입력을 확인하고 싶습니다. – Ktown

+0

'새 RegExp ("\ b (hate | notCool) \ b", "gi")'대신 사용해야합니다. –

관련 문제