2013-09-04 3 views
1

연락처 양식의 유효성을 검사하는 데 사용하는이 함수가 있지만 JSHint를 통해 실행할 때 "정의되지 않은"오류가 발생합니다. 나는 꽤 jquery 멍청 하네. 그리고 나는 이것들이 어떻게 정의되어 져야하는지 잘 모르겠다. - '필요'정의되지 않은 폼 데이터에서JSHint "정의되지 않음"오류

:

(function ($, document, undefined) { 
$(document).ready(function(){ 

    // Place ID's of all required fields here. 
    required = ["name", "email", "message"]; 
    // If using an ID other than #email or #error then replace it here 
    email = $("#email"); 
    errornotice = $("#error"); 
    // The text to show up within a field when it is incorrect 
    emptyerror = "Please fill out this field."; 
    emailerror = "Please enter a valid e-mail."; 

    $("#theform").submit(function(){ 
     //Validate required fields 
     for (i=0;i<required.length;i++) { 
      var input = $('#'+required[i]); 
      if ((input.val() == "") || (input.val() == emptyerror)) { 
       input.addClass("needsfilled"); 
       input.val(emptyerror); 
       errornotice.fadeIn(750); 
      } else { 
       input.removeClass("needsfilled"); 
      } 
     } 
     // Validate the e-mail. 
     if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) { 
      email.addClass("needsfilled"); 
      email.val(emailerror); 
     } 

     //if any inputs on the page have the class 'needsfilled' the form will not submit 
     if ($(":input").hasClass("needsfilled")) { 
      return false; 
     } else { 
      errornotice.hide(); 
      return true; 
     } 
    }); 

    // Clears any fields in the form when the user clicks on them 
    $(":input").focus(function(){  
     if ($(this).hasClass("needsfilled")) { 
      $(this).val(""); 
      $(this).removeClass("needsfilled"); 
     } 
    }); 
}); 
})(jQuery, document); 

는 여기에 몇 가지 예입니다.

required = ["name", "email", "message"]; 

'i'는 정의되어 있지 않습니다.

for (i=0;i<required.length;i++) { 

'이메일'이 정의되지 않았습니다.

if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) { 

답변

0

당신은 "VAR"로 변수를 정의해야합니다 :

var required = ["name", "email", "message"]; 
for(var i... 
0

글쎄, 이것은 아마도 궁극적으로 스타일의 문제이다. var 선언없이 변수를 사용하면 분명히 선언되지 않은 변수를 사용하고 있음을 의미합니다. 경우에 따라서는 전역 범위을 사용하여 변수를 생성하기 위해 수행됩니다. 물론 클리너 방식의 경우) 변수를 어딘가에 선언하여 외부에있는을 모두 var email;으로 선언 할 수 있습니다.