2013-07-17 31 views
0

當電子郵件地址無效時,它在IF中正常工作。然後,當我去並輸入一個有效的電子郵件時,ELSE應該觸發並清除錯誤的div。修復後的Jquery郵件驗證火災安全

<SCRIPT> 
$(document).ready(function(){ 

$("#email").blur(function(){ 
var emailReg = /^([\w-\.][email protected]([\w-]+\.)+[\w-]{2,4})?$/; 
if (!emailReg.test(email.value)) { 
    $("#theErrorDivID").html('Email Address Is Not Valid!'); 
    $('#email').css("background-color","red"); 
    $('#email').focus(); 
} 
else { 
    $("#theErrorDivID").html = ""; 
} 

}) 


    }); 
</SCRIPT> 

回答

0
$("#theErrorDivID").html = ""; 

應該

$("#theErrorDivID").html(""); 
0

Demo here

$(document).ready(function() { 
    $("#email").blur(function() { 
     var emailReg = /^([\w-\.][email protected]([\w-]+\.)+[\w-]{2,4})?$/; 
     if (!emailReg.test($("#email").val())) { 
      $("#theErrorDivID").html('Email Address Is Not Valid!'); 
      $('#email').css("background-color", "red"); 
      $('#email').focus(); 
     } else { 
      $("#theErrorDivID").html(""); 
     } 

    }) 
}); 

在,如果你試圖.test()email.value,你應該有$("#email").val()(除非被一個定義的電子郵件變量之前);在其他你應該有.html("")而不是.html = "";