1
votes

Depending upon the responses from the user when they complete a form, controls may or may not be required. All of these controls have validation attached to them. For instance, if the user hasn't lived at their address for at least 2 years, I need to validate previous address information; if not, I need to skip that validation.

I've tried disabling the validator control with jQuery and although when the validation text is 'greyed out', it still causes validation.

<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>
2
Maybe the answer to this previous question will help. stackoverflow.com/questions/2379445/…SeaSharp

2 Answers

1
votes

Instead of your RequiredFieldValidator, use a CustomValidator instead.

So for example you can change your 'rfvAddressType' requiredfieldvalidator as follows:

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

Then on the server code:

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;   
}

More details here:

-2
votes

Try disabling or enabling validators on the run to achieve your desired functionality. You can try this code. I haven't tested it.

//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);