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

SOLUTION
Avatar of Randy Downs
Randy Downs
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of 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
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
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"
@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";
                }
@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
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