WDD5 - HTML: forms and input types
- I can build a simple HTML form using the required Higher input controls.
- I can explain how
id,name, labels and input types support form design. - I can apply HTML validation for range, length, presence and pre-selected data.
- I can use
<form>,<input>,<select>,<textarea>and submit controls correctly. - I can choose between
text,number,radio,textareaandsubmitfor a given field. - I can add
required,min,max,minlength,maxlength,checkedandselectedwhere appropriate.
WDD3 planned the workshop enquiry form. WDD5 implements its controls in HTML.
<label> to its matching <input>?Key vocabulary
Building forms
Required Higher input controls
The mandatory Higher input list is text, number, textarea, radio and submit. A form may also use <select> and <option> for drop-down choices. The date input type is useful in real websites, but it is extension-only in this course, not core Higher content.
Short typed text, such as school name or teacher email.
Numeric values, such as class size.
One choice from a small group of options.
Longer free-text answers.
A drop-down list where one option is chosen.
A button that sends the form.
Validation types
HTML can check simple rules before a form is submitted: presence with required, range with min and max, length with minlength and maxlength, and pre-selected data with checked or selected.
Worked examples
<form action="send.html">
<label for="schoolName">School name</label>
<input type="text" id="schoolName" name="schoolName" required maxlength="60">
<label for="classSize">Class size</label>
<input type="number" id="classSize" name="classSize" required min="1" max="33">
<input type="submit" value="Send enquiry">
</form>
<p>Workshop type</p>
<input type="radio" id="habitats" name="workshop" value="habitats" checked>
<label for="habitats">Habitats</label>
<input type="radio" id="adaptations" name="workshop" value="adaptations">
<label for="adaptations">Adaptations</label>
name, so only one can be selected.checked makes "Habitats" pre-selected.<label for="yearGroup">Year group</label>
<select id="yearGroup" name="yearGroup" required>
<option value="">Choose a year group</option>
<option value="p5">Primary 5</option>
<option value="p6" selected>Primary 6</option>
<option value="p7">Primary 7</option>
</select>
<label for="notes">Additional notes</label>
<textarea id="notes" name="notes" maxlength="300"></textarea>
selected attribute can pre-select data when appropriate.Choose the best control and validation for each field: teacher email, class size, workshop type, extra notes and submit button.
- Teacher email:
input type="text",required, sensiblemaxlength - Class size:
input type="number",required,minandmax - Workshop type: radio buttons sharing the same
name - Extra notes:
<textarea>withmaxlength - Submit button:
input type="submit"
- Using JavaScript for this lesson's validation. Higher form validation here is HTML attributes:
required,min,max,minlength,maxlength,checkedandselected. - Giving radio buttons different names. Radio buttons in one group must share the same
name. - Forgetting an empty first option in a required select. Without it, the browser may accept the first real option automatically.
- Using
type="date"as core content. It is useful, but extension-only for this course. - Confusing
idandname.ididentifies one element;nameis the field name submitted with the form.
If asked about form validation, name the validation type and the matching HTML attribute: presence uses required, range uses min/max, length uses minlength/maxlength, and pre-selected data uses checked or selected.
Task Set A - Core questions
min max.required to a <select> whose first option is <option value="p5">Primary 5</option>, with no empty option before it. What problem does this cause?Task Set B - Extension
type="date" might be useful in the workshop form, and why it is not treated as core Higher content here.<input type="radio"> for "Yes" and a separate, differently-named <input type="radio"> for "No", intended to be mutually exclusive options for the same question. Explain what is wrong with this and how to fix it.required and maxlength provide client-side validation, checked in the browser before the form is even submitted. Explain one reason a real website might still repeat similar checks on the server after the form is submitted, even though the client-side check already ran.File this in OneNote under:
Higher Computing Science > Web Design & Development > WDD5
Suggested timing: 8 min warm up, 18 min controls and vocabulary, 25 min worked examples, 15 min now-you-try/live coding, 35 min Task Set A, remaining time Task Set B.
Scope control: validation here is HTML attribute validation, not JavaScript. JavaScript begins in WDD11.
Date input: keep type="date" in Task Set B only. It is not in the mandatory Higher input list.