Take the 2-minute tour ×

Just wondering, will it be possible to assign the request validation to asp:TextBox with jquery/javascript?

I have the following code will create a checkbox with a textbox next to it:

        <th class="graytext r">Add Reps to Team:</th>
         <asp:GridView ID="grid" runat="server" AutoGenerateColumns="False" DataKeyNames="EmployeeID"
                DataSourceID="dsEmployees" EnableViewState="false"
                GridLines="None" CssClass="clGridDirectory">
                  <asp:TemplateField >
                      <asp:CheckBox runat="server" ID="employee_name" CssClass="employee_name" Text='<%# Eval("fullname") %>'/> 
                      <asp:HiddenField runat="server" ID="employeeidToRep" Value='<%# Eval("employeeid") %>'/>
                      <asp:TextBox runat="server" ID="repID" Text='<%# Eval("rep_id") %>'  />
           <asp:SqlDataSource ID="dsEmployees" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
                SelectCommand="app_staff_without_team_select" SelectCommandType="StoredProcedure">

Just wondering, will it be possible for me to assign the asp:RequiredFieldValidator to the textbox when checkbox is checked or removed the asp:RequiredFieldValidator when checkbox is unchecked?

share|improve this question

2 Answers 2

ASP.NET renders a client-side function named ValidatorEnable that you can use to enable/disable a validator on the fly. Just called it from the CheckBox's click event:

$(function() {
    $('#checkBoxID').click(function() {
        var validator = document.getElementById('validatorId');
        ValidatorEnable(validator, $(this).prop('checked'));
share|improve this answer

Set the CssClass attribute on the TextBox, such as CssClass="RepClass" and try to disable the required field validator in the 'click' event:

so...something like this...


not 100% this will work, but worth a try!

share|improve this answer
-1: I don't like to down-vote, but there's a few problems with this solution: 1) If you apply the class to the TextBox, then $(".RepClass").click will attach an event handler to the TextBox, not the CheckBox; 2) If the function did work, it would operate on all of the controls at once, not just one at a time; 3) Finally, the function would only disable. It wouldn't reenable, which is a requirement. –  FishBasketGordo Jun 29 '11 at 1:24
That's why I said not 100% sure this will work... I don't have time to actually test all of this... it was just a suggestion. You shouldn't vote down in a situation where the user says 'not 100% sure'. –  Zunandi Jun 29 '11 at 1:30
Like I said, I hate to down-vote, and I wouldn't have if you had been almost right, but this answer has a lot of problems. If you are not a 100% sure of an answer, a better place for your input would be in the comments section. –  FishBasketGordo Jun 29 '11 at 1:37

Your Answer


