Home > ASP.Net, Javascript > Validating a Textbox when a CheckBox is checked

Validating a Textbox when a CheckBox is checked


Hi

try this example:

<script language="javascript">

        function RequirePersonalDetails(source, args)
        {           

            var chk = document.getElementById('chkRequired');

            var Name = document.getElementById('txtName');
            if (chk.checked)
            {
                if (Name.value.length <= 0)
                {
                    args.IsValid = false;
                }
                else
                {
                    args.IsValid = true;
                }
            }
            return;       
        }

    </script>

<asp:CheckBox ID="chkRequired" runat="server" Text="Required Personal Details" />

<asp:TextBox ID="txtName" runat="server"></asp:TextBox>

    <asp:CustomValidator ID="CustomValidator1" runat="server"
        ErrorMessage="Enter Your Name" ControlToValidate="txtName"
        ClientValidationFunction="RequirePersonalDetails"
        ValidateEmptyText="true">
    </asp:CustomValidator>

Good Luck

Advertisements
Categories: ASP.Net, Javascript
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: