The required attribute of the <select> element is to let users know that the <select> drop-down is required and need to be submitted before the form is submitted. If you won't select the value from the drop-down and try to submit the form, it won't submit and a warning would be visible on the web page itself.
Definition and Usage The required attribute is a boolean attribute. When present, it specifies that an input field must be filled out before submitting the form. Note: The required attribute works with the following input types: text, search, url, tel, email, password, date pickers, number, checkbox, radio, and file.
1. Required attribute: If you want to make an input mandatory to be entered by the user, you can use the required attribute. This attribute can be used with any input type such as email, URL, text, file, password, checkbox, radio, etc. This can help to make any input field mandatory.
If the input elements aren't inside a form tag then HTML5 required attribute will fail to work. So always put input tags inside a form along with the form encapsulation itself.
Mandatory: Have the first value empty - required works on empty values
Prerequisites: correct html5 DOCTYPE and a named input field
<select name="somename" required>
<option value="">Please select</option>
<option value="one">One</option>
</select>
As per the documentation (the listing and bold is mine)
The required attribute is a boolean attribute.
When specified, the user will be required to select a value before submitting the form.If a select element
- has a required attribute specified,
- does not have a multiple attribute specified,
- and has a display size of 1 (do not have SIZE=2 or more - omit it if not needed);
- and if the value of the first option element in the select element's list of options (if any) is the empty string (i.e. present as
value=""
),- and that option element's parent node is the select element (and not an optgroup element),
then that option is the select element's placeholder label option.
The <select>
element does support the required
attribute, as per the spec:
Which browser doesn’t honour this?
(Of course, you have to validate on the server anyway, as you can’t guarantee that users will have JavaScript enabled.)
Yes, it's working:
<select name="somename" required>
<option value="">Please select</option>
<option value="one">One</option>
</select>
you have to keep first option blank.
You can use the selected
attribute for the option element to select a choice by default. You can use the required
attribute for the select element to ensure that the user selects something.
In Javascript, you can check the selectedIndex
property to get the index of the selected option, or you can check the value
property to get the value of the selected option.
According to the HTML5 spec, selectedIndex
"returns the index of the first selected item, if any, or −1 if there is no selected item. And value
"returns the value of the first selected item, if any, or the empty string if there is no selected item." So if selectedIndex = -1, then you know they haven't selected anything.
<button type="button" onclick="displaySelection()">What did I pick?</button>
<script>
function displaySelection()
{
var mySelect = document.getElementById("someSelectElement");
var mySelection = mySelect.selectedIndex;
alert(mySelection);
}
</script>
<form action="">
<select required>
<option selected disabled value="">choose</option>
<option value="red">red</option>
<option value="yellow">yellow</option>
<option value="green">green</option>
<option value="grey">grey</option>
</select>
<input type="submit">
</form>
You need to set the value
attribute of option
to the empty string:
<select name="status" required>
<option selected disabled value="">what's your status?</option>
<option value="code">coding</option>
<option value="sleep">sleeping</option>
</select>
select
will return the value
of the selected option
to the server when the user presses submit
on the form
. An empty value
is the same as an empty text
input -> raising the required
message.
w3schools
The value attribute specifies the value to be sent to a server when a form is submitted.
Example
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