troubleshooting Question

avoid validation when disabled... javascript ... asp 4

Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America asked on
5 Comments1 Solution255 ViewsLast Modified:
I have the following line of code:

<script type="text/javascript">
            window.onload = function () {
                document.getElementById("<%= chkEmailUpdates.ClientID %>").onchange = function () {
                    var txtAddress = document.getElementById("<%= txtAddress.ClientID %>");
                    txtAddress.disabled = !this.checked;
                    if (!this.checked) txtAddress.value = "";
                    if (this.checked) txtAddress.focus();
                    txtAddress.setAttribute('CausesValidation = " + txtAddress """');
to enables or disables txtAddress based on the value of a chkEmailUpdates.

Question: How can have txtAddress to be validated only when it is enabled?

Thank you.

Followings are the tags for the check box and txtAddress:

                <td class="style14">
                    Monthly updates?</td>
                <td class="style13">
                <td class="style6">
                    <asp:CheckBox ID="chkEmailUpdates" runat="server" Text="Email me updates"  />
                <td class="style14">
                    Email Address:</td>
                <td class="style13">
                    <asp:RequiredFieldValidator ID="requireEmail" runat="server" 
                        ControlToValidate="txtAddress" CssClass="style8" 
                        ErrorMessage="Email is required." Display="Dynamic" ValidationGroup="updateRequestEmail" >*</asp:RequiredFieldValidator>
                    <asp:RegularExpressionValidator ID="validEmail" runat="server" 
                        ErrorMessage="Acceptable format is:" 
                <td class="style6">
                    <asp:TextBox ID="txtAddress" runat="server"  
                        placeholder="Email required w/ update request only" Width="230px" 
                        Enabled ="false"></asp:TextBox>
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
            txtAddress.Enabled = chkEmailUpdates.Checked
            ' If chkEmailUpdates.Checked Then Page.Validate("updateRequestEmail")
    End Sub
Obadiah Christopher
Developer User Interface

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros