Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<select> required not working

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>
like image 545
Jibin Avatar asked Jun 02 '17 07:06

Jibin


People also ask

Why required is not working in select tag?

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.

Does required work on select?

The required attribute works only on empty values. So, you must leave the first value of the <select> element empty.

Can we use required in select tag?

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.

How do you make select Options mandatory?

Put "obligatory" at the end, before the comma.


2 Answers

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.

like image 94
sensorario Avatar answered Oct 12 '22 20:10

sensorario


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
like image 25
Adhan Timothy Younes Avatar answered Oct 12 '22 20:10

Adhan Timothy Younes