Part of my HTML form looks like this:
<p>Please select your preferred time slot:</p>
<input type="checkbox" id="am" name="am" value="AM">
<label for="am">AM (09:00-13:00)</label><br>
<input type="checkbox" id="pm" name="pm" value="PM">
<label for="pm">PM (13:00-18:00)</label><br>
I’d like for at least one of the checkboxes to be selected as a requirement of the form being submitted. I’d also like the option of both boxes being ticked at the same time. At the moment, if I leave both unselected, the form is still submitted.
I considered using the required property but I can’t figure out how to utilise it without making both required.
2
Answers
type
from"checkbox"
to"radio"
[name]
value (ex. all arename="m"
)required
to only one of them.Just check on input whether any of the checkboxes is selected. If not – reselect the changed checkbox: