Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey Everyone!!

I've one checkbox:-

<asp:CheckBox ID="CheckBox1" runat="server" Checked="true" Width="140px" />Save My Details and Call me for Assistance




I've one button:-
<asp:Button ID="Button4" type="button" runat="server" Text="Compare Rates" OnClientClick="callme();" />


My Jquery functions are in as:-

C#
<script type="text/javascript">

        $(function callme() {
            $('#CheckBox1').click(function () {
                if ($(this).is(':checked')) {
                    uservalid();
                }
            });
        });


        function uservalid() {
            var name, mobile, mail, query;
            name = document.getElementById("ContentPlaceHolder1_text10").value;
            mobile = document.getElementById("ContentPlaceHolder1_text11").value;
            mail = document.getElementById("ContentPlaceHolder1_text12").value;
            query = document.getElementById("ContentPlaceHolder1_text13").value;
            emailExp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([com\co\.\in])+$/;

            if (name == "" && mobile == "" && mail == "" && query == "") {
                alert("Enter All Fields Mendatory!!");
                return false;
            }

            else if (name == "") {
                alert("Enter Name");
                document.getElementById("ContentPlaceHolder1_Text10").setAttribute("style", "border:solid 1px red;background-color:#E2D3DD");
            }

            else if (mobile == "") {
                alert("Enter Mobile");
                document.getElementById("ContentPlaceHolder1_Text11").setAttribute("style", "border:solid 1px red;background-color:#E2D3DD");
            }

            else if (mail == "") {
                alert("Enter Mail ");
                document.getElementById("ContentPlaceHolder1_Text12").setAttribute("style", "border:solid 1px red;background-color:#E2D3DD");
            }

            else if (mail != '') {
                if (!mail.match(emailExp)) {
                    alert("Invalid Email Id");
                    return false;
                }
                return true;
            }

            else if (query == "") {
                alert("Enter your Query");
                document.getElementById("ContentPlaceHolder1_Text13").setAttribute("style", "border:solid 1px red;background-color:#E2D3DD");
            }

        }
        </script>


Now I want if checkbox is checked onbutton click function callme() is executed which in turn calls function uservalid(). But I'm not able to do it. Please Help!!
Posted
Comments
Have you debugged and checked that it is going inside "if ($(this).is(':checked')) {"?
Deepak Kanswal Sharma 19-Jun-15 12:25pm    
How can I see that step by step process?

That's because in your callme function you then wireup the click event to your checkbox.

Instead you want to check if it is checked:
JavaScript
if($("#CheckBox1").is(':checked'))
 
Share this answer
 
Comments
Deepak Kanswal Sharma 19-Jun-15 12:18pm    
Even this is not working:-

function callme() {
if($('#CheckBox1').is(':checked'))
uservalid();
};
Sergey Alexandrovich Kryukov 19-Jun-15 14:55pm    
Check up if you selector is correct. Are you sure it has id="CheckBox1" and this id value is unique on page? You can check it up looking at the page source code on the client site, not your ASP.NET file. Also make sure that your observation is correct, that the function under "if" is really not called. Insert some debugging code instead of the call.
—SA
ZurdoDev 19-Jun-15 16:52pm    
This code looks fine so as SA says, you'll have to debug to see what is wrong.
Sergey Alexandrovich Kryukov 19-Jun-15 14:55pm    
5ed.
—SA
Deepak Kanswal Sharma 19-Jun-15 22:51pm    
Oh yeah baby!! It worked. Actually on page source code I found the ID was '#ContentPlaceHolder1_CheckBox1' not '#CheckBox1'.

Got It!!

Thanks for your support! ;)
Your need to write a onchange/onclick event for a check box and within this if the condition satisfies you need to call the button click event using jQuery "trigger" event to validate.

$("#Button4").trigger("click")

Here is the link I worked out for you

http://jsfiddle.net/0967qgo1/[^]
 
Share this answer
 
v2
Comments
Deepak Kanswal Sharma 19-Jun-15 12:30pm    
Thanks buddy, but this isn't my question.
Sorry if I haven't made it clear.

What I want is if checkbox is checked, it should call the function uservalid() onclientclick of button "compare rates"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900