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

Bypass validation when chkbox = false, javascript.... asp 4

The code below is javascript to show/hide txtComment based on chkAddComment value. This part works fine.

Now that I have added requireComment validation to txtComment, I need to upgraded this javascript code to bypass validation when chkAddComment  is false.

Question: How this is done?

Thank you.

<script type="text/javascript">

            // to anonymous function are created to help with chkBox functionalities
            window.onload = function () {

                // on document first-time load chkAddComment is false therefore it has to be set to none
                document.getElementById("CommentRow").style.display = (document.getElementById("<%= chkAddComment.ClientID %>").checked) ? "block" : "none";
                
                document.getElementById("<%= chkAddComment.ClientID %>").onchange = function () {
                    if (this.checked) {
                        document.getElementById("CommentRow").style.display = "block";
                    } else {
                        document.getElementById("CommentRow").style.display = "none";
                    }
                }
            }
</script>

Open in new window

ASP.NETJavaScript

Avatar of undefined
Last Comment
Mike Eghtebas

8/22/2022 - Mon
SOLUTION
Randy Downs

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
Number-1,

I don't see any reference to validation issue I am having. What you intend to do with your solution?

Thanks,

Mike
ASKER CERTIFIED SOLUTION
Rajar Ahmed

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
Randy Downs

Sorry, I thought your validation would go in the block that you have working. My If would only run your block of code when chkAddComment="True"
Obadiah Christopher

@meeran03...

In this code, validateComment() line won't be required, I guess. Coz it will otherwise show an alert each time the checkbox is checked.

if (this.checked) {
                    document.getElementById("CommentRow").style.display = "block";
                    validateComment();
                } else {
                    document.getElementById("CommentRow").style.display = "none";
                }
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Rajar Ahmed

@informaniac ,

  Yes, i understand but the author userinterface was like that i could not know how e's going to validate so i added .  For that reason only , i have created a button on commentrow control so that validation can be performed while submitting also.

Meeran03
Mike Eghtebas

ASKER
Hi @informaniac and informaniac,

I don't want any alert box to come up. I have a ValidationSummary1 to handle the
messaging.

Please note that, I have already a Save button. I will not add a new submit button. But I will include:

OnClientClick="return validateComment()"

with the Save button's other attributes.

I need some to help make the following pseudo code work:

if <chkAddComment= true> then
  'add ControlToValidate="txtComment" to ID="txtComment"  tag using DOM
Else
  'remove ControlToValidate="txtComment" from ID="txtComment"  tag using DOM
End if

I have a similar code but line 13 never fires:
document.getElementById("<%= chkEmailUpdates.ClientID %>").onchange = function () {
      //alert("a");
      var txtAddress = document.getElementById("<%= txtAddress.ClientID %>");
      txtAddress.disabled = !this.checked;
       if (!this.checked) txtAddress.value = "";
       if (!this.checked) txtAddress.focus();
       //txtAddress.setAttribute('CausesValidation = " + txtAddress """');
        if (!this.checked) {
              //txtAddress.setAttribute('CausesValidation = "txtName"');
              alert("txtName"); //
               } else {
                   txtAddress.setAttribute('CausesValidation = " + txtAddress """');
                    alert("txtAddress"); // 
                }
                    
 }

Open in new window

Thank you,

Mike