Pergunta

<asp: CompareValidator runat="server" ID="RequiredFieldValidator5" 
      ControlToValidate="TextBox13" 
      ValidationExpression="RadioButtonList5.Text == 'No'">
    Please Answer Question
</asp:RequiredFieldValidator>`

I would like to setup a CompareValidator for a text box, based upon the response from a RadioButton.

I have a web page where the user is supposed to answer several questions. If the user answers no to question 5 (RadioButtonList5 Yes/No) then in question 6 they are supposed to enter a date.

I know the ValidationExpression is incorrect. I just need help figuring out how to set it up properly.

Foi útil?

Solução

Since user input determines if the compare validator is enabled or not, then you need to use JavaScript/jQuery code, like this:

// This will enable the validator
ValidatorEnable(document.getElementById("RequiredFieldValidator5"), true);

// This will disable the validator
ValidatorEnable(document.getElementById("RequiredFieldValidator5"), false);

So you will need to run the above code in a change handler for the radio button list, like this:

$("#<%=RadioButtonList5.ClientID%> input").change(function() {
    if($(this).val() == "Yes") {
        // Enable or disable compare validator
    }
    if($(this).val() == "No") {
        // Enable or disable compare validator
    }
});
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top