HTML Intermediate: Structure, Tables and Forms
More Form Controls and Validation
In this lesson you will
- Use textarea, select and fieldset
- Add built-in validation with required, minlength and pattern
- Explain why validation must also happen on a server
Introduction
A text box is not the only control. In this lesson you will meet the rest of the family, and learn how HTML itself can check what a visitor typed, without any JavaScript.
Textarea and select
textareais a multi-line text box, good for messages.selectis a drop-down list. Each choice is anoption.
A textarea has a closing tag, because its content is the starting text.
Quick quiz
Which element makes a drop-down list?
Grouping with fieldset
fieldset groups related controls and legend names the group. It is very helpful for radio buttons, because screen readers announce the legend before each option.
Built-in validation
Add attributes to a control and the browser checks it before sending:
required: the field cannot be empty.minlengthandmaxlength: limits on text length.minandmax: limits on numbers and dates.type="email": must look like an email address.pattern: a regular expression the text must match.
Press Join with the fields empty or wrong. In most browsers you will see a message from the browser itself. (The result panel blocks real sending, so a valid form simply does nothing.)
Quick quiz
Which attribute stops a form being sent while a field is empty?
Validation is not security
Browser checks are friendly, but anyone can bypass them, for example by editing the page or sending data directly. A real website must check the data again on its server. Treat HTML validation as help for the visitor, never as protection.
Try it yourself
Add a number input called age that is required and only allows values from 5 to 18. Then press Join with an age of 40 to see the message.
Summary
textarea,select/optionandfieldset/legendcomplete your form toolkit.required,min,max,minlength,maxlength,patternand special types give you free checks.- Always validate again on a server. Browser validation is not security.
Lesson complete
+20 XP
Nice work. Keep going while it is fresh.
Sources and further reading