The checkboxes work before enhancement. Selection-count validation needs JavaScript; if the module doesn’t load, follow the limits in the legend. Applications must also validate selections on the server.
Manual registration
The class-only import doesn’t register the element. Call
customElements.define() once before using it.
import { FormRequiredCheckboxesElement } from
'https://esm.sh/@aarongustafson/form-required-checkboxes@3.2.7/form-required-checkboxes.js?bundle&target=es2022';
if (!customElements.get('form-required-checkboxes')) {
customElements.define(
'form-required-checkboxes',
FormRequiredCheckboxesElement
);
}
<form>
<form-required-checkboxes required="2-3">
<fieldset>
<legend>Select 2 or 3 toppings</legend>
<label><input type="checkbox" name="toppings" value="pepperoni"> Pepperoni</label>
<label><input type="checkbox" name="toppings" value="mushroom"> Mushroom</label>
<label><input type="checkbox" name="toppings" value="olive"> Olive</label>
<label><input type="checkbox" name="toppings" value="onion"> Onion</label>
</fieldset>
</form-required-checkboxes>
<button type="submit">Submit</button>
<button type="reset">Reset</button>
</form>