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>
The required attribute is there in the HTML select element. If it is not working for you, then try checking back the “first value” field. You need an empty value there.
The required attribute works only on empty values. So, you must leave the first value of the <select> element empty.
The <select> element has some unique attributes you can use to control it, such as multiple to specify whether multiple options can be selected, and size to specify how many options should be shown at once. It also accepts most of the general form input attributes such as required , disabled , autofocus , etc.
Put "obligatory" at the end, before the comma.
First of all, replace
<option value=" ">--Select Category--</option>
with
<option value="">--Select Category--</option>
And then, as w3c says, ... "The required attribute is a boolean attribute. When specified, the element is required."
<select name="category" required class="form-control">
<option value="">--Select Category--</option>
<option value="asd">asd</option>
</select>
Also here you can find same example.
And here you can try the code.
Required is working. The fact is that " " is a value and "" is not.
Try to remove space value in your select category option
<select name="category" required="required" class="form-control">
<option value="">--Select Category--</option>
<option value="asd">asd</option>
</select> code here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With