2013-07-19 49 views
0

我有以下javascript代碼來驗證用戶名和密碼的簡單登錄表單 - 這是一個asp.net自定義驗證程序的客戶端驗證,如果這有所幫助。jQuery - 只有在所有表單字段都有效的情況下才能運行加載圖形函數

function userValidation(source, arguments) { 
     if (arguments.Value != "" && arguments.Value != "User name") { 
      if($(source).parents(".loginPanel").find(".errorAsterisk[style*='visible']").length==0) { 
       $(source).parents(".loginPanel").css({ "background-color": "" }); 
      } 
      arguments.IsValid = true; 
     } else { 
      $(source).parents(".loginPanel").css({ "background-color": "#ECC8C8" }); 
      arguments.IsValid = false; 
     } 
    } 


    function passwordValidation(source, arguments) { 
     var passVal = /^((?![<>]).){1,64}$/; 
     if (arguments.Value != "" && arguments.Value != "Password" && passVal.test(arguments.Value)) { 
      if($(source).parents(".loginPanel").find(".errorAsterisk[style*='visible']").length==0) { 
       $(source).parents(".loginPanel").css({ "background-color": "" }); 
      } 
      arguments.IsValid = true; 
     } else { 
      $(source).parents(".loginPanel").css({ "background-color": "#ECC8C8" }); 
      arguments.IsValid = false; 
     } 
    } 

我也希望當用戶成功地填補了形式出現「正在加載」消息 - 這是我對這些代碼...

$(".loginPanel").on("click", "input[id*='Login']", function() { 
     loadingPage2("", "Logging in..."); 
    }); 

麻煩的是,加載功能即使頁面無效,也會運行。

任何想法,我可以包括它,所以它只運行,如果的一切有效?

感謝

回答

2

沒有seing更多的代碼,你可以這樣做:

var x = []; 
function userValidation(source, arguments) { 
     if (arguments.Value != "" && arguments.Value != "User name") { 
      if($(source).parents(".loginPanel").find(".errorAsterisk[style*='visible']").length==0) { 
       $(source).parents(".loginPanel").css({ "background-color": "" }); 
      } 
      arguments.IsValid = true; 
      x.push(true); 
     } else { 
      $(source).parents(".loginPanel").css({ "background-color": "#ECC8C8" }); 
      arguments.IsValid = false; 
     } 
    } 


    function passwordValidation(source, arguments) { 
     var passVal = /^((?![<>]).){1,64}$/; 
     if (arguments.Value != "" && arguments.Value != "Password" && passVal.test(arguments.Value)) { 
      if($(source).parents(".loginPanel").find(".errorAsterisk[style*='visible']").length==0) { 
       $(source).parents(".loginPanel").css({ "background-color": "" }); 
      } 
      arguments.IsValid = true; 
      x.push(true); 
     } else { 
      $(source).parents(".loginPanel").css({ "background-color": "#ECC8C8" }); 
      arguments.IsValid = false; 
     } 
    } 

function loading(){ 
    var confirmx = true; 
    for (var i = 0; i < x.length; i++) { 
     if (x[i] != true) { 
      confirmx = false; 
     } 
    } 
    if (confirmx){ 
     $(".loginPanel").on("click", "input[id*='Login']", function() { 
      loadingPage2("", "Logging in..."); 
     }); 
    } 
} 
loading(); // run it! 

你需要檢查的唯一的事情是,這個代碼的所有驗證功能後運行。所以把這段代碼放在腳本的末尾。

+0

嗯,我擔心你會這樣說,因爲我有很多其他地方需要添加加載圖形。是否可以將var設置爲數組並檢查數組中的所有條目是否爲真? – Tom

+1

如果您不需要知道哪個值爲false,請使用:var x = []'並添加每個函數'x.push(true);'。你需要知道哪個值失敗了嗎? (如果我知道的話,我可以更新答案) – Rikard

+0

我只需要知道某件事是無效的...... – Tom

0

如何實現這一點有很多方法。最簡單的就是設置一個全局變量e.q. success。在事件處理程序中,只需檢查此變量的值。

相關問題