I have checkbox List Control it has more than 300+ items generated
How to check atleast one ChcekBox is selected or not using JavaSCript
And
How to limit checkbox should select only 15 out of 300 and if they try to select more than 15 it should not allow . Using Javascript Please can you help?
If you are using jQuery, this could be accomplished by using :
//This will check that at least one checkbox is checked
$("input[type='checkbox']:checked").length > 0
//This will check if more than 15 are checked
$("input[type='checkbox']:checked").length > 15
Related Javascript :
<script type='text/javascript'>
$(document).ready(function(){
//Checks when a checkbox is checked
$('input[type="checkbox"]').click(function(){
//If more than 15 are checked - don't allow
if (this.checked && $('input:checked').length > 15) {
event.preventDefault();
alert('You can only check 15 checkboxes');
}
});
});
</script>
For a pure Javascript solution, you could create a function to check the number of checkboxes that are checked and attach this event to the onclick property of your checkboxes :
<script type='text/javascript'>
function countCheckboxes(){
var inputElems = document.getElementsByTagName("input");
var count = 0;
for (var i=0; i<inputElems.length; i++) {
if (inputElems[i].type === "checkbox" && inputElems[i].checked === true) {
count++;
}
}
if(count > 15){
alert("You can only select 15 checkboxes");
event.preventDefault();
}
}
</script>
using the following markup (example) :
<input type='checkbox' onclick='countCheckboxes();' />
<!-- Additional Checkboxes Here -->
They keep giving you jQuery, which is based on JavaScript, which is not allowed as I understood...
Here is a code-behind example (use it on postback event):
YOu can use this for example:
/// <summary>
/// Finds all controls of type T stores them in FoundControls
/// </summary>
/// <typeparam name="T"></typeparam>
private class ControlFinder<T> where T : Control
{
private readonly List<T> _foundControls = new List<T>();
public IEnumerable<T> FoundControls
{
get { return _foundControls; }
}
public void FindChildControlsRecursive(Control control)
{
foreach (Control childControl in control.Controls)
{
if (childControl.GetType() == typeof(T))
{
_foundControls.Add((T)childControl);
}
else
{
FindChildControlsRecursive(childControl);
}
}
}
}
// this code wasn't compiled and run. Check and adjust as nesessary:
var checkboxFinder = new ControlFinder<checkbox>();
var atleast1Checked = checkboxFinder.FoundControls.Any(p=>p.Checked);
more here: http://stackoverflow.com/questions/4955769/better-way-to-find-control-in-asp-net
Alexei Fimine
_____________
"And though I have the gift of prophecy, and understand all mysteries, and all knowledge; and though I have all faith, so that I could remove mountains, and have not charity, I am nothing"
If you read title correctly then question is about how to make check using JavaScript.
Of course, data must be validated also on server side because user can easily download some extension to browser that allows tampering of data that is about to be sent to server. Tampering happens when data starts going out from browser and on JavaScript
level everything is done what can be done. One example of such extnesions is here: https://addons.mozilla.org/en-US/firefox/addon/tamper-data/
Don't forget to mark solution providing post as "Answered".
aparitala
Member
13 Points
83 Posts
How to check atleast one ChcekBox is selected or not using JavaSCript
Jan 28, 2013 04:14 PM|LINK
I have checkbox List Control it has more than 300+ items generated
How to check atleast one ChcekBox is selected or not using JavaSCript
And
How to limit checkbox should select only 15 out of 300 and if they try to select more than 15 it should not allow . Using Javascript Please can you help?
Rion William...
All-Star
27148 Points
4504 Posts
Re: How to check atleast one ChcekBox is selected or not using JavaSCript
Jan 28, 2013 04:25 PM|LINK
jQuery Solution
If you are using jQuery, this could be accomplished by using :
//This will check that at least one checkbox is checked $("input[type='checkbox']:checked").length > 0 //This will check if more than 15 are checked $("input[type='checkbox']:checked").length > 15Related Javascript :
<script type='text/javascript'> $(document).ready(function(){ //Checks when a checkbox is checked $('input[type="checkbox"]').click(function(){ //If more than 15 are checked - don't allow if (this.checked && $('input:checked').length > 15) { event.preventDefault(); alert('You can only check 15 checkboxes'); } }); }); </script>Example
Pure Javascript Solution
For a pure Javascript solution, you could create a function to check the number of checkboxes that are checked and attach this event to the onclick property of your checkboxes :
<script type='text/javascript'> function countCheckboxes(){ var inputElems = document.getElementsByTagName("input"); var count = 0; for (var i=0; i<inputElems.length; i++) { if (inputElems[i].type === "checkbox" && inputElems[i].checked === true) { count++; } } if(count > 15){ alert("You can only select 15 checkboxes"); event.preventDefault(); } } </script>using the following markup (example) :
Example
DigiMortal
Contributor
5658 Points
939 Posts
MVP
Re: How to check atleast one ChcekBox is selected or not using JavaSCript
Jan 28, 2013 04:26 PM|LINK
Also visit my ASP.NET blog or follow me @ Twitter:twitter.com/gpeipman
fimine
Contributor
3008 Points
549 Posts
Re: How to check atleast one ChcekBox is selected or not using JavaSCript
Jan 28, 2013 04:33 PM|LINK
They keep giving you jQuery, which is based on JavaScript, which is not allowed as I understood...
Here is a code-behind example (use it on postback event):
YOu can use this for example:
/// <summary> /// Finds all controls of type T stores them in FoundControls /// </summary> /// <typeparam name="T"></typeparam> private class ControlFinder<T> where T : Control { private readonly List<T> _foundControls = new List<T>(); public IEnumerable<T> FoundControls { get { return _foundControls; } } public void FindChildControlsRecursive(Control control) { foreach (Control childControl in control.Controls) { if (childControl.GetType() == typeof(T)) { _foundControls.Add((T)childControl); } else { FindChildControlsRecursive(childControl); } } } }// this code wasn't compiled and run. Check and adjust as nesessary:
var checkboxFinder = new ControlFinder<checkbox>();
var atleast1Checked = checkboxFinder.FoundControls.Any(p=>p.Checked);
_____________
"And though I have the gift of prophecy, and understand all mysteries, and all knowledge; and though I have all faith, so that I could remove mountains, and have not charity, I am nothing"
DigiMortal
Contributor
5658 Points
939 Posts
MVP
Re: How to check atleast one ChcekBox is selected or not using JavaSCript
Jan 28, 2013 05:34 PM|LINK
If you read title correctly then question is about how to make check using JavaScript.
Of course, data must be validated also on server side because user can easily download some extension to browser that allows tampering of data that is about to be sent to server. Tampering happens when data starts going out from browser and on JavaScript level everything is done what can be done. One example of such extnesions is here: https://addons.mozilla.org/en-US/firefox/addon/tamper-data/
Also visit my ASP.NET blog or follow me @ Twitter:twitter.com/gpeipman