2016-05-25 130 views
1

我一直試圖讓驗證我的文本框和唯一可行的事情是設置工作驗證文本框時,另一個文本框包含文本

autoPostBack=true 

,並在回發textbox_change事件添加

textBox3.Validator.Enabled = true; 

但我注意到,chrome和邊緣dosent總是觸發回發事件,然後驗證從未激活。對於像添加驗證這樣的小事情做回發並不方便用戶使用。它必須是一個更簡單的方法..

所以我得到這個

<asp:TextBox runat="server" ID="txt1"/> <--- Dont need validation on that one, when starting to type values are populated from a ajax call 
<asp:TextBox runat="server" ID="txt2"/> <--- Here i want to add validation when txt1 contains something, no validation should fire if the user steps in on txt1 and then out without writing text 
<asp:RequiredFieldValidator ID="txt2Validator" runat="server" Enabled="false" ErrorMessage="" Display="Static" SetFocusOnError="True" ValidationGroup="valgroup1" ControlToValidate="txt2"/> 
<asp:DropDownList ID="ddl1" runat="server" /> <--- also validate this only when txt1 contains text 
<asp:RequiredFieldValidator ID="ddl1Validator" runat="server" Enabled="false" ErrorMessage="" Display="Static" SetFocusOnError="True" ValidationGroup="valgroup1" ControlToValidate="ddl1"/> 

那麼,有沒有在jQuery的辦法所需的字段驗證設置爲Enabled =當txt1中包含的文字可能有真正的onblur事件?

部分解決

行,所以我是有點快,不要以爲我自己...... 我掙扎了一整天找我的Ajax請求的錯誤和時得到固定,我是做出來的思考果汁,只是放棄了,問了這個問題。但是,由於知道這個肩膀是一個問題,我嘗試了編碼,結果與此。有用!是! 我唯一不會做的事情是,如果我可以讓它只在txt1包含文本時添加驗證,現在驗證啓用爲兒子,因爲我在txt1中點擊,然後再次出來。有沒有,如果this.txt1.contains.text那麼事情與jQuery?

<script type=text/javascript> 
    $(document).ready(function() { 
     $("#<%= txt1.ClientID %>").blur(function() { 
      ValidatorEnable(document.getElementById('<%=txt2Validator.ClientID%>'), true); 
      ValidatorEnable(document.getElementById('<%=ddl1Validator.ClientID%>'), true); 
     }); 
    }); 
</script> 

回答

0

OK ....再快問新的問題..

解決我的問題:

<script type=text/javascript> 
    $(document).ready(function() { 
     $("#<%= txt1.ClientID %>").blur(function() { 
      if ($("#<%= txt1.ClientID %>").val().length > 0) { 
       ValidatorEnable(document.getElementById('<%=txt2Validator.ClientID%>'), true); 
       ValidatorEnable(document.getElementById('<%=ddl1Validator.ClientID%>'), true); 
      } 
     }); 
    }); 
</script> 

也許不是最好的aproatch但它的作品。

所以這個問題就是......當你知道答案的時候,爲什麼要問一個問題? 也許它可以幫助別人。 (哎呀又做了,回答了我自己的問題)