1

根据用户填写表单时的响应,可能需要也可能不需要控件。所有这些控件都附加了验证。例如,如果用户没有在他们的地址居住至少 2 年,我需要验证以前的地址信息;如果没有,我需要跳过该验证。

我尝试使用 jQuery 禁用验证器控件,尽管当验证文本“变灰”时,它仍然会导致验证。

<script src="../../Scripts/jquery-1.6.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $('#ddlSpouseData').change(
            function () {
                var selection = $('#<%= ddlAddressData.ClientID%>').get(0).selectedIndex;
                if (selection == 1) {
                    alert(selection);
                    $('#rfvAddressType').attr('disabled', 'disabled');
                    $('#rfvBAddress').attr('disabled', 'disabled');
                }

            });
    });
</script>

       <table id="MyTable" class="tableModule">
           <tr>
                <td class="tableDataBorder">
                    Data is not available and<br /> will be provided later.
                </td>
                <td class="tableDataBorder">
                    <asp:DropDownList ID="ddlAddressData" runat="server">
                        <asp:ListItem>Please Select</asp:ListItem>
                        <asp:ListItem>Yes</asp:ListItem>
                        <asp:ListItem>No</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr class="hideRow">
                <td class="tableDataBorder">
                    Identifying Document
                </td>
                <td class="tableDataBorder">
                    <asp:DropDownList ID="ddlAddressType" runat="server">
                        <asp:ListItem>Please Select</asp:ListItem>
                        <asp:ListItem>Home</asp:ListItem>
                        <asp:ListItem>Office</asp:ListItem>
                        <asp:ListItem>Other</asp:ListItem>
                    </asp:DropDownList>
                    <br />
                    <asp:RequiredFieldValidator ID="rfvAddressType" CssClass="regexError" ControlToValidate="ddlAddressType"
                        runat="server" ErrorMessage="Please select address type" Display="Dynamic"
                        InitialValue="Please Select"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr class="hideRow">
                <td class="tableDataBorder">
                   Address
                </td>
                <td>
                    <asp:TextBox ID="txtAddress" MaxLength="50" runat="server" CssClass="StdTxtBox"></asp:TextBox><br />
                    <asp:RequiredFieldValidator ID="rfvBAddress" CssClass="regexError" ControlToValidate="txtAddress"
                        runat="server" ErrorMessage="Please enter your address" Display="Dynamic"></asp:RequiredFieldValidator>
                </td>
            </tr>
            </table>
4

2 回答 2

1

而不是您的RequiredFieldValidator,而是使用CustomValidator

因此,例如,您可以按如下方式更改“rfvAddressType” requiredfieldvalidator:

<asp:CustomValidator runat="server" 
     id="valAddressType" 
     controltovalidate="ddlAddressType" 
     onservervalidate="cusCustom_ServerValidate" 
     errormessage="Please select address type" />

然后在服务器代码上:

protected void cusCustom_ServerValidate(object sender, ServerValidateEventArgs e)
{
        if (ddlAddressType.SelectedItem.Value == "1")             
            e.IsValid = false; // or do any additional validation checks here!    
        else            
            e.IsValid = true;   
}

更多细节在这里:

于 2012-08-02T00:38:30.360 回答
-2

尝试在运行时禁用或启用验证器以实现您想要的功能。你可以试试这段代码。我没有测试过。

//Syntax:
ValidatorEnable(ValidatorContronName,Boolean);
//Explanation:ValidatorContronName - This is ClientID of the Validation control.
Boolean - true(Enable) / false(Disable)
//Example:
ValidatorEnable(document.getElementById('<%=rfvAddressType.ClientID%>'), false);
于 2012-08-01T21:25:45.820 回答