문제

<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.

도움이 되었습니까?

해결책

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
    }
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top