<select> required not working

Viewed 25776

I'am using this code along with php code for a select and used required class to make it mandatory but it is not working. Does anyone can help me.I have included this html section along with php code.

 <select name="category" required="required" class="form-control">
        <option value=" ">--Select Category--</option>
        <option value="asd">asd</option>
    </select>
6 Answers

You have to add required and value attributes

<select required="true" name="unit">
<option disabled selected value="">-- Select One --</option>
<option>Square Feet</option>
<option>Square Yards</option>
<option>Square Meters</option>
<option>Marla</option>
<option>Kanal</option>

In my case, the required attribute on the select element wasn't working because the first option element had the selected attribute added.

selected attribute broke the required attribute

<label for="procedures_of_interest">Procedures Of Interest</label>
<select name="procedures_of_interest" id="procedures_of_interest" required multiple="multiple">
  <option value="" selected disabled>Select One or More</option>
  <option value="first">first</option>
  <option value="second">second</option>
  <option value="last">last</option>
</select>

By removing the the selected attribute from the option element, the required and multiple attributes both worked correctly on the select element:

<label for="procedures_of_interest">Procedures Of Interest</label>
<select name="procedures_of_interest" id="procedures_of_interest" required multiple="multiple">
  <option value="" disabled>Select One or More</option>
  <option value="first">first</option>
  <option value="second">second</option>
  <option value="last">last</option>
</select>
Related