0

I want to make only one checkbox of a group of checkboxes mandatory for the user to check without using JavaScript. I have the following (quite simple) code:

<input type="checkbox" class="checkbox_condition" name="checkboxGroupName" id="first" required>
<input type="checkbox" class="checkbox_condition" name="checkboxGroupName" id="first" required>

I want to compel visitor to select one checkbox from the group. How can I do that without JavaScript? I would appreciate any recommendations.

Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
Petya Naumova
  • 727
  • 2
  • 11
  • 23

1 Answers1

1

Switch your checkbox to radio - radio buttons have the built in function you are looking for, in your css you can make the radio button look like a check box. see my fiddle https://jsfiddle.net/pcvwwjoe/2/

<input type="radio" class="checkbox_condition" name="checkboxGroupName" id="first" required>
  <input type="radio" class="checkbox_condition" name="checkboxGroupName" id="first"> 

also, notice that since both radio buttons have the same name, you only need to use required once since they are all grouped together.

Adam Buchanan Smith
  • 9,422
  • 5
  • 19
  • 39