Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag 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

Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

ValidatorEnable

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

ValidatorEnable(txtAddress,!txtAddress.disabled);

Open in new window

Avatar of Mike Eghtebas

ASKER

How do you include it in the javascrip code submitted?
ASKER CERTIFIED SOLUTION
Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 guess it should be
ValidatorEnable(txtAddress, !(txtAddress.disabled));

and not

ValidatorEnable(txtAddress, !txtAddress.disabled);