The required attribute is used to make a <select> dropdown mandatory before form submission. It ensures that the user selects an option from the dropdown list.
- Prevents form submission if no option is selected.
- Commonly used in forms where selection is compulsory.
- It is a boolean attribute, so its presence alone makes the dropdown required.
syntax:
<select required> Example: Illustrates the use of required attribute in select element.
<!--Driver Code Starts-->
<!DOCTYPE html>
<html>
<head>
<title>HTML | <select> required Attribute</title>
<style>
h1,
h2 {
color: green;
font-style: italic;
}
body {
text-align: center;
}
</style>
</head>
<body>
<h1>GeeksForGeeks</h1>
<h2>HTML select required attribute</h2>
<form action="">
<!--Driver Code Ends-->
<select required>
<option value="">None</option>
<option value="ds">Data Structure</option>
<option value="algo">Algorithm</option>
<option value="os">Operating System</option>
<option value="cn">Computer Network</option>
</select>
<!--Driver Code Starts-->
<input type="submit">
</form>
</body>
</html>
<!--Driver Code Ends-->