How to Make Checkbox Required in HTML
In this tutorial, you will learn how to make checkbox required in HTML. HTML checkboxes are used to let the users select one or more options on a given checkbox group. The checkboxes are shown square in shape when enabled. Sometimes when you are creating a form, you may want to create checkboxes that are must be selected (checked) by the users before submitting the form.
To make the checkbox required, we have to use the HTML
required attribute. The
required attribute is a Boolean attribute, which is used to specify that the user must select the required option before submitting the form. This required attribute can also be used in other form inputs like radio or text.
In the following example, we have 3 checkboxes and we will make one of them required. Please have a look over the code example and the steps given below.
- In the HTML file, we have multiple elements such as
input. The parent
divelement is just a wrapper for the rest of the elements.
h2heading tags have some text content.
- We have 3 inputs with type value set to
checkboxto select the course. The first checkbox has a
requiredattribute, which specifies that the user must select this option before submitting the form.