How to Make a asp:Textbox required only IF a checkbox is checked

Posted on 2011-04-19
Last Modified: 2012-05-11
I have 1 checkbox and 2 textboxes.
I need to make the two textboxes required only when the check box is checked.
How do I achieve this? I included the javascript and asp code I could get so far, but it
doesn't seem to be working fine.
<asp:CheckBox ID="chkTraining" onclick="updateValidator();" runat="server" />&nbsp;&nbsp;
<asp:TextBox ID="txtDate" runat="server"></asp:TextBox>&nbsp;
<asp:Label ID="lblScore" runat="server" Text="Score:"></asp:Label>
<asp:TextBox ID="txtScore" runat="server" MaxLength="3"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvTraining" ControlToValidate="txtDate" ErrorMessage="Please enter the Date" runat="server"></asp:RequiredFieldValidator>

Open in new window

<script type="text/javascript">
function updateValidator() {
            var enableValidator = !event.srcElement.status;
            var rfvTraining = document.getElementById('rfvTraining');
            ValidatorEnable(rfvTraining, enableValidator);

Open in new window

Question by:luisr69
    LVL 8

    Expert Comment

    Use the on Checkbox checked event.

    LVL 17

    Accepted Solution

    Hello, try this:
    <%@ Page Language="C#" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
    <script runat="server">
        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
            if (CheckBox1.Checked && args.Value.Trim().Length == 0)
                args.IsValid = false;
    <html xmlns="">
    <head runat="server">
        <script type="text/javascript">
            function validateTextBox(sender, args) {
                var myCheckBox = document.getElementById('CheckBox1');
                if (myCheckBox.checked && args.Value.trim().length == 0) {
                    args.IsValid = false;
        <form id="form1" runat="server">
            <asp:CheckBox ID="CheckBox1" runat="server" ClientIDMode="Static" />
            <br />
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="CustomValidator"
                ForeColor="Red" ClientValidationFunction="validateTextBox" ControlToValidate="TextBox1"
                ValidateEmptyText="true" OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
            <br />
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            <asp:CustomValidator ID="CustomValidator2" runat="server" ErrorMessage="CustomValidator"
                ForeColor="Red" ClientValidationFunction="validateTextBox" ControlToValidate="TextBox2"
                ValidateEmptyText="true" OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
            <br />
            <asp:Button ID="Button1" runat="server" Text="Button" />

    Open in new window

    Or this:

    Author Closing Comment

    Worked like a charm!
    Thanks a lot.
    LVL 17

    Expert Comment

    by:Carlos Villegas
    Good to know buddy!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now