Click here to Skip to main content
15,886,795 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all I am having a form with 2 check-boxes and a button what I need to do is if the user clicks on a button with our checking any check-box available, I would like to show custom alert saying select one.. If one is selected I don't want to show the alert...

for this I used the following

http://www.abeautifulsite.net/blog/2008/12/jquery-alert-dialogs/[^]

with some changes as follows

XML
<script type="text/javascript">

        $(document).ready(function () {
            var atLeastOneIsChecked = $('#checkArray :checkbox:checked').length > 0;

            if (atLeastOneIsChecked == 0) {
                $("#alert_button").click(function () {
                    jAlert('This is a custom alert box', 'Alert Dialog');
                });
            }
        });

    </script>


XML
<form id="form1" runat="server">
    <fieldset id="checkArray">
    <input type="checkbox" name="chk[]" value="Apples" />
    <input type="checkbox" name="chk[]" value="Bananas" />
</fieldset>
        <p>
            <input id="alert_button" type="button" value="Show Alert" />
        </p>
    </form>


But I am getting alert on both the case can any one help me..
Posted
Comments
Sergey Alexandrovich Kryukov 8-Feb-12 4:39am    
Wow!
--SA

1 solution

try this

JavaScript
<script type="text/javascript">
 
        $(document).ready(function () {
            $("#alert_button").click(function () {
            	var atLeastOneIsChecked = $('#checkArray :checkbox:checked').length > 0;
 
            	if (atLeastOneIsChecked == 0) {
                    jAlert('This is a custom alert box', 'Alert Dialog');
                
            	}
	    });
        });
 
    </script>
 
Share this answer
 
Comments
demouser743 8-Feb-12 4:48am    
Thanks CRDave
CRDave1988 8-Feb-12 4:51am    
welcome
demouser743 8-Feb-12 4:53am    
Hi small help how can I handle when I am referring to the check boxes that are in grid-view
CRDave1988 10-Feb-12 0:19am    
use <%=id.clintID%>

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