仅在显示时验证字段

时间:2015-08-26 12:16:53

标签: javascript jquery asp.net

我有asp.net webform我有一个隐藏字段,当用户选择单选按钮时会显示该字段。我遇到的问题是,我需要将此字段设置为强制字段,只有当它显示为其他方面我才不希望它验证。

这样做的最佳方式是什么,以及我该如何做到这一点。

隐藏字段的HTML

<div class="form-group">
    <asp:Label ID="Step03PgSelectionLabel" class="col-sm-4 control-label" runat="server" Text="How many pages would you like us to look at *" style="padding-top: 0px"></asp:Label>
    <div class="col-sm-3">
        <asp:Label runat="server" class="radio-inline" style="padding-top: 0px">
            <asp:RadioButton runat="server" id="Step03AllPgsRadioButton" value="All" GroupName="Step03PgSelection" onclick="$('#hiddenSpecificPages').hide();" />All
        </asp:Label>
        <asp:Label runat="server" class="radio-inline" style="padding-top: 0px">
            <asp:RadioButton runat="server" id="Step03SelectionPgsRadioButton" name="selection" value="Selection" GroupName="Step03PgSelection" onclick="$('#hiddenSpecificPages').show();" />Selection
        </asp:Label>
    </div>
    <div class="col-sm-offset-4 col-sm-8">
        <asp:CustomValidator id="CustomStep03PgSelection" runat="server" Display="Dynamic" ForeColor="Red" ErrorMessage="Please select if you would like us to look at all you pages or specific pages." ClientValidationFunction="Step03PgSelection_ClientValidate" OnServerValidate="Step03PgSelection_ServerValidate" />
    </div>
</div>
    <div class="form-group" id="hiddenOtherField">
        <asp:Label ID="Step03OtherFieldLabel" class="col-sm-4 control-label" runat="server" Text="Please specify *" AssociatedControlID="Step03OtherField"></asp:Label>
        <div class="col-sm-4">
            <asp:TextBox ID="Step03OtherField" runat="server" class="form-control" style="max-width: 100%"></asp:TextBox>
        </div>
        <div class="col-sm-offset-4 col-sm-8">
            <asp:RequiredFieldValidator Display="Dynamic" runat="server" ID="reqStep03OtherField" SetFocusOnError="true" ForeColor="Red" ControlToValidate="Step03OtherField" ErrorMessage="Please specify your website's type." />
        </div>
    </div>

1 个答案:

答案 0 :(得分:0)

您可以使用jquery检查该字段是否可见。

$(element).is(":visible");