[Last Call] Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2011-04-19
Medium Priority
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
  • 2

Expert Comment

ID: 35429216
Use the on Checkbox checked event.

LVL 17

Accepted Solution

Carlos Villegas earned 2000 total points
ID: 35429251
Hello, try this:
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        if (CheckBox1.Checked && args.Value.Trim().Length == 0)
            args.IsValid = false;
<html xmlns="http://www.w3.org/1999/xhtml">
<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

ID: 35429419
Worked like a charm!
Thanks a lot.
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35429423
Good to know buddy!

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Loops Section Overview
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month18 days, 6 hours left to enroll

829 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