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.
Guarded registration
Importing define.js registers the element when
supported. Calling its helper again is safe.
import { defineFormRequiredCheckboxes } from
'https://unpkg.com/@aarongustafson/form-required-checkboxes@latest/define.js?module';
defineFormRequiredCheckboxes();
<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>