2012-10-23 23 views
-1

我的Default.aspx頁面上有許多文本框,類似於下面的內容。在單個ASP文本框中驗證jquery

<asp:TextBox ID="myTextbox" runat="server"></asp:TextBox>

當用戶點擊該按鈕提交,下面的JavaScript執行:

  $(function() { 
      $('#<%= myButton.ClientID %>').click(function (clickToExecuteMyMethod) { 
       var userWantsToSubmit = window.confirm("Are you sure you want to press the button?"); 
       if (userWantsToSubmit) { 
        $.blockUI({ overlayCSS: { backgroundColor: '#00f' }, message: '<h1>Please wait a while...</h1>' }); 
       } 
       if (!userWantsToSubmit) { 
        clickToExecuteMyMethod.preventDefault(); 
       } 
      }); 
     });  

但我還想要使用jQuery驗證的東西(什麼)已被輸入到myTextbox一旦按下相同的按鈕。如果它驗證成功,那麼我想要另一個JavaScript來觸發。

回答

2

用於檢查單個文本框剛剛得到的文本框的值與.val()

$('#<%= myButton.ClientID %>').click(function (clickToExecuteMyMethod) { 
    var userWantsToSubmit = window.confirm("Are you sure you want to press the button?"); 

    // Check to see if the textbox is empty 
    var isValid = $('#<%= myTextbox.ClientID %>').val() != ""; 

    if (userWantsToSubmit && isValid) { 
     $.blockUI({ overlayCSS: { backgroundColor: '#00f' }, message: '<h1>Please wait a while...</h1>' }); 
    } else { 
     clickToExecuteMyMethod.preventDefault(); 
    } 
}); 

這不會是一個非常靈活的解決方案,雖然的簡單情況。我建議你看看jquery validation plugin之類的東西,或者嘗試Google搜索其他驗證解決方案,周圍有很多。

+0

我會嘗試實施了jQuery驗證插件。不過,我現在能夠獲得此代碼的工作。 – Krondorian

0

如果你使用任何ASP.Net頁面驗證控件,你可以調用客戶端驗證程序:

$(function() { 
    $('#<%= myButton.ClientID %>').click(function (clickToExecuteMyMethod) { 
     var userWantsToSubmit = window.confirm("Are you sure you want to press the button?"); 
     Page_ClientValidate('validationGroup'); //validate using ASP.Net validator controls. 
     if (userWantsToSubmit && Page_IsValid) { 
      $.blockUI({ 
       "overlayCSS": { 
        "backgroundColor": "#00f" 
       }, 
       "message": "<h1>Please wait a while...</h1>" 
      }); 
     } 
     if (!userWantsToSubmit || !Page_IsValid) { 
      clickToExecuteMyMethod.preventDefault(); 
     } 
     return Page_IsValid; 
    }); 
}); 
+0

我還沒有任何asp.net頁面驗證控件,但會考慮添加它們。 – Krondorian