1

I am trying to make a simple validation on some agreements by checking an asp:checkbox and clicking an asp:button.

On my Button I am calling this:

OnClientClick="ValidateConditions();"

And the function runs by this:

function ValidateConditions() {
 if ($('#ChkAccept').is(':checked')) {
             alert("Checked");
                  args.IsValid = true;
               }
                else {
                   alert('Nope it is not checked');
                args.IsValid = false;
           }
         }

It works so far, but how do I stop running my button method when args.IsValid = false; ? Right now it shows the message and runs the method in both cases.

It must be a simple thing that I need.. Best regards.

1
  • Any progress with the new code? Commented Nov 26, 2012 at 12:43

3 Answers 3

1

do the below to prevent the event of the button :-- but do also OnClientClick="return ValidateConditions(this); in your markup code

 function ValidateConditions(element,e) {
    if ($(element).prop('checked')) {
        alert("Checked");
        args.IsValid = true;
    }
    else {
        alert('Nope it is not checked');
        args.IsValid = false;
e.preventDefault();
e.stopPropagation();
return false ;
    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, I an trying your code, but still it runs my server side click handler: Protected Sub BtnConfirm_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnConfirm.Click 'Code that runs End Sub
please make sure you write the correct call to the function ( OnClientClick="return ValidateConditions(this,event);
0

Try:

OnClientClick="return ValidateConditions(this);"

and

function ValidateConditions(checkbox) {
    return $(checkbox).is(':checked');
}

1 Comment

Hi, thanks for your reply. I am trying your solution. Nothing happens when I clicking the button, both when the checkbox is checeked and unchecked. But at least it doesn't run my code behind.
0

Removed all other javascripts:

Just use:

   <asp:CheckBox ID="ChkAccept" runat="server" ClientIDMode="Static" Text="I accept the conditions" /> 
    <br />
   <asp:Button ID="BtnConfirm" runat="server" CssClass="uniBtn" Text="Send"
   OnClientClick="if(! $( '#ChkAccept' ).prop( 'checked' )){alert('Not Ticked');return false;}" />  

6 Comments

Hi, thanks for your reply. I am trying your solution, but in this case it says "Nope it is not checked" in both cases, and it continues to run the code behind.
Hi, nope, it just shows the message and then runs my button click handler: Protected Sub BtnConfirm_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnConfirm.Click 'Code that runs End Sub
It's just simple: <asp:CheckBox ID="ChkAccept" runat="server" Text="Please accept the conditions" />
Hi, it's still running the code. I have made a small example for download here: dl.dropbox.com/u/17202645/test.aspx.zip
Now my method only runs when the checkbox is ticked! Nice. Now I just need the alert to show up if it is not checked.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.