2013-08-22 27 views
-2

我正在進行表單驗證,所有字段都應該被要求,並且檢查用戶是否「接受條款」,並檢查電子郵件是否正確。它幾乎可以工作,如果我沒有輸入姓名/電子郵件等,這段代碼給了我一個迴應,但如果我輸入這個信息,它不會隱藏().alert。然後不回覆條款和「有效的電子郵件」檢查複選框。如果沒有「響應」,.alert也應該始終「隱藏」。表單驗證if/else與JQuery

此代碼在哪裏失敗?有什麼想法嗎?

$("form").submit(function (e) { 
e.preventDefault(); // This will prevent the form submission 

    var response = ""; 
    $('#submit-holder').find('input').each(function(){ 
     if ($(this).val() == '') { 
     response += ", " + $('label[for="' + this.id + '"]').html(); 
     empty_fields = true;  
     }  

     else if ($(this).val() == !'') { 
     empty_fields = false; 
      $('.alert').hide() 
     } 

     else if(empty_fields = true) { 
        $('.alert').addClass('alert-danger') 
        $('.alert').fadeIn()    
        $('.error_message').text(response.replace(", ", "You need to fill out: ")) 
        message = response.replace(", ", "You need to fill out: ") 
        response_message(message) 
     } 

     else if ($('[name="txtEmail"]').length !== 0) { 

      var email = $('[name="txtEmail"]').val(); 

      if(IsEmail(email)==false){ 
        message = "The e-mail address you've entered is invalid"; 
        response_message(message) 
         //return false; 
        } 
      } 

      else if($('#user_terms').not(":checked")){ 
      message = "You need to accept the terms and conditions to continue"; 
      response_message(message) 
       //return false; 

      } 

     }); 

if($('#user_terms').is(":checked") && !response) { 

    // Send the form 

} 


// Functions: 
function IsEmail(email) { 
    var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; 
    if(!regex.test(email)) { 
     return false; 
    }else{ 
     return true; 
    } 
    } 

function response_message(message) { 

    $('.alert').addClass('alert-danger') 
    $('.alert').fadeIn()    
    $('.error_message').text(message) 
}  
+0

否則,如果($(本).VAL()==「」!){ - 看起來像一個罪犯...... –

+2

看起來好像你缺少了一些行結束分號; – RedEyedMonster

+0

[JQuery表單不會驗證成功]的可能重複(http://stackoverflow.com/questions/18379864/jquery-form-wont-validate-to-success) – mplungjan

回答

0

爲什麼不使用驗證器插件。

我使用http://jqueryvalidation.org/

+0

更多的評論,而不是答案。 –

+0

是的我知道,但我在這裏是新的..我沒有足夠的權限/信譽評論...對不起:( –

+0

這可能是好的,但我不想太多「插件我不知道」 – Kim

0

嗨這段代碼沒有錯誤的工作,但我不知道你的邏輯是正確的。

$("form").submit(function (e) { 
e.preventDefault(); // This will prevent the form submission 

    var response = ""; 
    $('#submit-holder').find('input').each(function(){ 
     if ($(this).val() == '') { 
     response += ", " + $('label[for="' + this.id + '"]').html(); 
     empty_fields = true;  
     }  

     else if ($(this).val() != '') { 
     empty_fields = false; 
      $('.alert').hide(); 
     } 

     else if(empty_fields = true) { 
        $('.alert').addClass('alert-danger'); 
        $('.alert').fadeIn();   
        $('.error_message').text(response.replace(", ", "You need to fill out: ")); 
        message = response.replace(", ", "You need to fill out: "); 
        response_message(message); 
     } 

     else if ($('[name="txtEmail"]').length !== 0) { 

      var email = $('[name="txtEmail"]').val(); 

      if(IsEmail(email)==false){ 
        message = "The e-mail address you've entered is invalid"; 
        response_message(message); 
         //return false; 
        } 
      } 

      else if($('#user_terms').not(":checked")){ 
      message = "You need to accept the terms and conditions to continue"; 
      response_message(message); 
       //return false; 

      } 

     }); 

if($('#user_terms').is(":checked") && !response) { 

    // Send the form 

} 


// Functions: 
function IsEmail(email) { 
    var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; 
    if(!regex.test(email)) { 
     return false; 
    }else{ 
     return true; 
    } 
    } 

function response_message(message) { 

    $('.alert').addClass('alert-danger'); 
    $('.alert').fadeIn();   
    $('.error_message').text(message); 
}