Magento - Validate checkbox
-
30-09-2020 - |
Solution
I have add common hidden input field to the whole Grid checkbox column, that is below :
<input type="hidden" id="checkBoxesStatus" class="input required-entry" value="" />
and add the below script for checkboxes validation :
<script type="text/javascript">
//<![CDATA[
var validateCheckBoxes = (function() {
var checkBoxesStatus = false;
$$('#adminformsGrid tbody tr .checkbox').each(function(e) {
if(e.checked) {
checkBoxesStatus = true;
}
});
if(checkBoxesStatus) {
$('checkBoxesStatus').value = 1;
} else {
$('checkBoxesStatus').value = '';
}
});
$$('#adminformsGrid tbody tr .checkbox').invoke('observe','click', function() {
validateCheckBoxes();
});
$$('#adminformsGrid thead tr .sp-pointer').invoke('observe','click', function() {
validateCheckBoxes();
});
It will work...
OTHER TIPS
You can try validate-one-required
class.
validate-one-required
tends for > At least one textbox/radio element must be selected in a group
To use the validate-one-required
validator you must first add the class name to only one checkbox/radio
button in the group (last one is probably best) and then place all the input elements within a parent element, for example a div element. That way the library can find all the checkboxes/radio
buttons to check and place the validation advice element at the bottom of the parent element to make it appear after the group of checkboxes/radio
buttons.
So you can use it like:
<fieldset>
<input class="checkbox" type="checkbox" name="checkbox[]" />
<input class="checkbox" type="checkbox" name="checkbox[]" />
<input class="checkbox" type="checkbox" name="checkbox[]" />
<input class="checkbox" type="checkbox" name="checkbox[]" />
<input class="checkbox validate-one-required" type="checkbox" name="checkbox[]" />
</fieldset>
For more check here : Easy Validation by Prototype