Home >Web Front-end >JS Tutorial >Can I use `required` Attribute for `` Fields in HTML?
The
Initially, the required attribute was not supported by
A viable workaround involves setting the first option value as an empty string (""). This triggers the required attribute to function as intended. By combining this method with a named input field and a valid HTML5 DOCTYPE, you can enforce selection for
<code class="html"><select name="somename" required> <option value="">Please select</option> <option value="one">One</option> </select></code>
To ensure this solution works effectively, adhere to these prerequisites:
As outlined in the official HTML5 documentation:
"When a select element has a required attribute specified, does not have a multiple attribute specified, has a display size of 1, and the first option element's value is an empty string, that option is the select element's placeholder label option."
The above is the detailed content of Can I use `required` Attribute for `` Fields in HTML?. For more information, please follow other related articles on the PHP Chinese website!