micknt
micknt

Reputation: 297

Validating a CheckBox field - ASP.NET and jQuery

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.

Upvotes: 1

Views: 1368

Answers (3)

Akhil Sekharan
Akhil Sekharan

Reputation: 12693

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

Upvotes: 0

Athamneh
Athamneh

Reputation: 298

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 ;
    }

}

Upvotes: 1

Lucian
Lucian

Reputation: 4001

Try:

OnClientClick="return ValidateConditions(this);"

and

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

Upvotes: 0

Related Questions