Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkbox validation - at least one selected

I have number of checkboxes and another checkbox for "Select All"

I want to check if the user has selected at least one checkbox. Need modification in javascript

<script language="Javascript">

function doSubmit(){
function check_checkboxes() 
{ 
checked=false;
var c = document.getElementsByTagName('INPUT'); 
for (var i = 1; i < c.length; i++) 
{ 
    if (c[i].type == 'checkbox') 
    { 
    if (c[i].checked) {
    return true} 
    else {alert("Please identify what warehouses comply:");  }
        } 
    } //if I place my struts action here..its not working?
}    
document.holiDay.command.value= 'addingApp'; //My Struts action if something checked. 
document.holiDay.submit(); 
}    
like image 564
Some Java Guy Avatar asked Jul 09 '10 15:07

Some Java Guy


People also ask

How do you validate that at least one checkbox is checked?

There is a form with multiple checkboxes and we're going to make sure that at least one is checked using pure JavaScript. To set a custom validation error message, we will use setCustomValidity() method.

How do I validate a checkbox?

Checking if a checkbox is checked First, select the checkbox using a DOM method such as getElementById() or querySelector() . Then, access the checked property of the checkbox element. If its checked property is true , then the checkbox is checked; otherwise, it is not.

How do you check if atleast one checkbox is selected in jQuery?

The trick is that you can use .is(":checked") on a jQuery object full of a bunch of elements and it'll return true if any of them are checked and false if none of them are. AND, using . attr() for the disabled attribute with that boolean value will enable/disable that button.

Does checkbox allow multiple selection?

The MultiSelect has built-in support to select multiple values through checkbox, when mode property set as CheckBox .


2 Answers

var all=document.getElementById('holiDay');

In HTML IDs should be unique, so getElementById will only return 1 element. Perhaps you could try getElementsByTagName - http://msdn.microsoft.com/en-us/library/ms536439(VS.85).aspx ?

Something like...

function check_checkboxes()
{
  var c = document.getElementsByTagName('input');
  for (var i = 0; i < c.length; i++)
  {
    if (c[i].type == 'checkbox')
    {
       if (c[i].checked) {return true}
    }
  }
  return false;
}

and change your Validate function to...

function Validate()
{
    if(!check_checkboxes())
    {
        alert("Please identify what warehouses comply:");  
        return false;
    }
    return true;
}
like image 161
barrylloyd Avatar answered Oct 01 '22 07:10

barrylloyd


Select at least one check box using jqQery. Try the following code.

$('input[type="checkbox"][name="class"]').on('change', function () {
    var getArrVal = $('input[type="checkbox"][name="class"]:checked').map(function () {
        return this.value;
    }).toArray();

    if (getArrVal.length) {
        //execute the code
    } else {
        $(this).prop("checked", true);
        alert("Select at least one column");
        return false;
    }
    ;
});
like image 20
Rupesh Wankhede Avatar answered Oct 01 '22 09:10

Rupesh Wankhede