Going beyond text input
In the previous lesson, you learned about basic form elements: <input> (text, email, number, checkbox, radio) and <button>. But real-world forms need more: dropdown menus, multi-line text areas, grouped form sections, and validation rules.
This lesson covers the advanced form elements that make forms user-friendly and professional. You'll learn how to organize forms with <fieldset>, collect multi-line input with <textarea>, let users pick from lists with <select>, validate data with attributes like pattern and minlength, and even provide autocomplete suggestions with <datalist>.
Select dropdowns with optgroup
The <select> element creates a dropdown menu. Users click it, see a list of <option> elements, and pick one.
Key attributes:
<select>— Container for the dropdown. Usenameto identify the field in form submission.<option>— A single choice. Thevalueattribute is sent to the server; the text is what users see.<optgroup>— Groups related options with alabel. Users can't select the label, only the options inside.
<option>--Select--</option> to prompt users to make a choice. This is especially important for mobile users.
Textarea and grouping with fieldset
The <textarea> element lets users enter multiple lines of text. The <fieldset> and <legend> elements group related form fields visually and semantically.
Key attributes for textarea:
rows— Number of visible lines (default is usually 2).cols— Approximate character width. Modern layouts ignore this; use CSS width instead.maxlength— Maximum characters allowed. The textarea won't let users type beyond this.placeholder— Hint text that disappears when the user starts typing.
The <fieldset> element groups related inputs, and <legend> provides a title for the group. Browsers usually draw a border around fieldsets, which helps organize large forms visually.
Form validation attributes
HTML5 includes built-in validation attributes. Instead of writing JavaScript to check inputs, you declare what's valid directly on the input.
Common validation attributes:
required— User must fill this field before submitting.min,max— For numbers, set the range.minlength,maxlength— For text, set character limits.pattern— A regex pattern the input must match. For example,^\d{3}-\d{3}-\d{4}$matches phone numbers like 123-456-7890.step— For numbers, the increment between valid values.step="0.01"allows decimals like 19.99.
Datalist: autocomplete suggestions
The <datalist> element pairs with an <input> to provide autocomplete suggestions. Users can still type anything, but they see pre-defined options as they type.
The list attribute on the input must match the id of the datalist. As users type, suggestions that match appear. This is more flexible than <select> because users can type custom values.
<select> when users must choose from a fixed list. Use <datalist> when you want to suggest options but allow custom input.
Try it yourself: Job application form
Edit this form to see how fieldsets organize sections, optgroup groups options, and textarea accepts multi-line text.
Try it yourself: Form validation
Fill this form and click Submit. The browser validates phone number (regex), age (min/max), and password (minlength) before allowing submission.
Output and progress elements
The <output> element displays the result of a calculation. The <meter> and <progress> elements visualize ranges and completion.
Output element
Use <output> to show calculation results, often updated by JavaScript:
Meter and progress
<meter> shows a measurement (like a gauage), and <progress> shows completion:
Task: Build a hotel booking form with these fields organized in fieldsets:
- Guest Details fieldset: name (text, required), email (email, required), phone (with pattern validation like 123-456-7890)
- Booking Details fieldset: room type (select with optgroups like "Standard" and "Luxury"), check-in date, check-out date, number of guests (number with min=1, max=6)
- Special Requests: textarea for guest notes
- Submit button
Add CSS styling to make fieldsets visible (borders, padding). Try changing values and clicking Submit to see browser validation in action.
Hint: Create two fieldsets with legend titles. For "room type", use optgroup with label="Standard" and label="Luxury". Add a textarea with rows="4". Use type="date" for check-in and check-out. Remember to add pattern to phone input: pattern="\d{3}-\d{3}-\d{4}"
Test your knowledge
Key takeaways
<select>with<option>creates dropdowns. Use<optgroup>to group related options.<textarea>accepts multi-line text input. Control size withrowsandcols, and limit length withmaxlength.<fieldset>and<legend>organize forms into logical sections, improving both UX and accessibility.- HTML5 validation attributes (
required,min,max,pattern,minlength) provide client-side validation. Always validate on the server too. <datalist>pairs with<input>to provide autocomplete suggestions without restricting user input.<output>,<meter>, and<progress>display calculated results and visual indicators.
You just built advanced forms.
Imagine what you'll build next.
Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses →