当另一个文本框包含文本时,验证文本框

时间:2016-05-25 18:54:26

标签: c# asp.net unobtrusive-validation client-side-validation

我一直在尝试对我的文本框进行验证,唯一有效的是设置

  autoPostBack=true 

并在回发textbox_change事件中添加

 textBox3.Validator.Enabled = true;

但是我注意到chrome和edge 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"/>

当txt1包含可能带有onblur事件的文本时,在jquery中有一种方法可以将必需的字段验证器设置为Enabled = true吗?

部分解决

好的,所以我有点快,不要为自己考虑...... 我一整天都在努力寻找我的ajax请求中的错误,当这个问题得到解决时,我已经没想到果汁,只是放弃了,并且问了这个问题。但是,由于知道这个肩膀是一个问题,我尝试了som编码并最终得到了这个。有用!是! 我唯一能做的就是如果我只能在txt1包含文本时添加验证,现在验证启用为儿子,因为我在txt1中然后再次出来。有没有if.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>

1 个答案:

答案 0 :(得分:0)

好的....再次快速提出一个新问题..

解决我的问题:

<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但它有效。

所以问题就是......当你知道答案时,为什么要问一个问题? 也许它可以帮助别人。 (哎呀再说一遍,回答了我自己的问题)

相关问题