Avatar of Mike Eghtebas
Mike Eghtebas
Flag for United States of America asked on

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

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 """');
                }
            }
</script>

Open in new window

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:

            <tr>
                <td class="style14">
                    Monthly updates?</td>
                <td class="style13">
                    &nbsp;</td>
                <td class="style6">
               
                    <asp:CheckBox ID="chkEmailUpdates" runat="server" Text="Email me updates"  />
                    
                </td>
            </tr>
            <tr>
                <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" 
                        ControlToValidate="txtAddress" 
                        ErrorMessage="Acceptable format is: name@domain.com" 
                        ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" 
                        Display="Dynamic">*</asp:RegularExpressionValidator>
                    </td>
                <td class="style6">
                    <asp:TextBox ID="txtAddress" runat="server"  
                        placeholder="Email required w/ update request only" Width="230px" 
                        Enabled ="false"></asp:TextBox>
                </td>
            </tr>

Open in new window

and
   
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

Open in new window

ASP.NETJavaScript

Avatar of undefined
Last Comment
Obadiah Christopher

8/22/2022 - Mon
Obadiah Christopher

ValidatorEnable

http://msdn.microsoft.com/en-us/library/aa479045.aspx

ValidatorEnable(txtAddress,!txtAddress.disabled);

Open in new window

Mike Eghtebas

ASKER
How do you include it in the javascrip code submitted?
ASKER CERTIFIED SOLUTION
Obadiah Christopher

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Mike Eghtebas

ASKER
I tried  ValidatorEnable(txtAddress, !txtAddress.disabled); but it didn't work. I also used another suggestion (shown below). It doesn't work neither.
                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 """');
                    //ValidatorEnable(txtAddress, !txtAddress.disabled);
                    var validator = $get('<%=requireEmail.ClientID %>');
                    validator.enable = true;
                    ValidatorUpdateDisplay(validator);
                }

Open in new window

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Obadiah Christopher

I guess it should be
ValidatorEnable(txtAddress, !(txtAddress.disabled));

and not

ValidatorEnable(txtAddress, !txtAddress.disabled);