Click here to Skip to main content
15,906,341 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I am having 3 different checkboxes in my webform.. through which different modules can be assigned to different level of users.

Now i want to validate those checkboxes that application should not let the user to leave all the checkboxes uncheck and atleast one checkbox should be cheked.


KInldy give your precious knowledge about that..

Regards,


NOTE:: Dont wana use Jquery for this... If some one could provide me option regarding some asp built in validator or custom validator than that would be more appropriate and helping..


:)
Posted
Updated 23-May-13 19:31pm
v2

Use JQuery to validate the CheckBoxes. Try this:
JavaScript
$('button').click(function () {
  var okey = false;
  $('input:checkbox').each(function () {
    if ($(this).is(':checked')) {
      okey = true;
      return false;
    }
  });
  if(!okey){
      alert('Select atleast one checkbox');
      return false;
  }
  else{return true;}
});



--Amit
 
Share this answer
 
Comments
VICK 24-May-13 1:31am    
Can this work be done through ASP Built in validator or Custom Validator???/
VICK 24-May-13 3:19am    
Sorry if question seems stupid...

but do i have to paste the above mentioned code as it is on the design page and it will gona stick to all checkboxes on that form... or do i have to edit some thing.. Kindly guide.. coz totally unexperienced in JQuery. :(
_Amy 24-May-13 3:23am    
Before starting you should have basic idea about JQuery. Yes it is quite similar to JavaScript. Add a JQuery reference to your project and start writing the codes. Check this:
jQuery Tutorial[^]
jQuery[^]

--Amit
VICK 24-May-13 3:26am    
Appreciated your help.. :)

But can you provide me the straight instructions to embed the above mentioned code in my work.. coz time is short... and i will check the tutorials etc later...

regards,
_Amy 24-May-13 3:29am    
Put the code in:
$(document).ready(function() {
// script goes here
});

in your page or js file and add the reference(See my second link in previous comment) of jquery to page.
.cs side

ChkListDrEducation.Attributes.Add("onclick", "CheckOne(event);");



C#
function CheckOne(e) {
          if (!e) e = window.event;
          var sender = e.target || e.srcElement;

          if (sender.nodeName != 'INPUT') return;
          var checker = sender;
          var chkBox = document.getElementById('<%= ChkListDrEducation.ClientID %>');
          var chks = chkBox.getElementsByTagName('INPUT');
          for (i = 0; i < chks.length; i++) {
              if (chks[i] != checker)
                  chks[i].checked = false;
          }
      }
 
Share this answer
 
Comments
VICK 24-May-13 1:32am    
Can this work be done through ASP Built in validator or Custom Validator??? :-/
VICK 24-May-13 3:17am    
Further I am not using a checkList.. rather I am using 3 different checkboxes...:(

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