ragun91
ragun91

Reputation: 65

Want to validate the check box in magento using magento validation

I want to set the required field for the check box i tried my coding

 <input class="checkbox required-entry" type="checkbox" name="checkbox[]" />
<input class="checkbox required-entry" type="checkbox" name="checkbox[]" />
<input class="checkbox required-entry" type="checkbox" name="checkbox[]" />
<input class="checkbox required-entry" type="checkbox" name="checkbox[]" />
<input class="checkbox required-entry" type="checkbox" name="checkbox[]" />

i check any one of the field and press submit button the remaining field shows "This is a required field" . I Want to set the required field in check box if i not check any one of the value the required message will show other wise if not mandatory

Thank You

Upvotes: 1

Views: 7163

Answers (3)

Mohamed23gharbi
Mohamed23gharbi

Reputation: 1780

You can use : validate-one-required-by-name as long the inputs share the same name (which is usually the case)

Upvotes: 0

clockworkgeek
clockworkgeek

Reputation: 37700

From Really Easy Field validation:

  • validate-one-required (At least one textbox/radio element must be selected in a group – see below*)

*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.

Therefore you could do this:

<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>

But since those checkboxes share the same name (this isn't documented but is in the source) you could also do:

<!-- Maybe these are columns and don't share a parent element -->
<div>
    <input class="checkbox" type="checkbox" name="checkbox[]" />
    <input class="checkbox" type="checkbox" name="checkbox[]" />
    <input class="checkbox validate-one-required-by-name" type="checkbox" name="checkbox[]" />
    <!-- This checkbox is lowest on the page so should have the message -->
</div>
<div>
    <input class="checkbox" type="checkbox" name="checkbox[]" />
    <input class="checkbox" type="checkbox" name="checkbox[]" />
</div>

Upvotes: 6

P0ZiTR0N
P0ZiTR0N

Reputation: 612

add required-entry to its class

<input type="checkbox" id="id" name="name" value="value" class="checkbox required-entry">

Upvotes: 3

Related Questions