Skip to content
Forms, data and quality / Foundation

Forms II

More complex inputs, validation and the behaviour of forms in the browser.

The question How do you handle more complex inputs and form behaviour?

10 min readLesson 09 of 20Read. Try. Understand.

Saved lessons, read marks and notes stay in this browser. Sign in to keep them on all your devices.

On this page

The question

Learning outcomes

By the end of this lesson, you can add radio buttons, checkboxes, select menus, text areas, date and number controls; group related choices with fieldset and legend; explain single versus multiple selection; and choose submit or reset controls deliberately.

Prerequisites and retrieval

Open 008's registration form. For each control, identify its visible label, unique ID, submission name, and current value. Then predict which piece of request data vanishes if you remove its name.

The idea

Terminology

  • Radio group: Same-named radio inputs where at most one can be checked at a time. — Source: WHATWG: Radio Button state)

  • Checkbox: An input in the checkbox state: a binary on/off control, or one member of a same-name set. — Source: WHATWG: Checkbox state)

  • Select: “The select element represents a control for selecting amongst a set of options.” — Source: WHATWG: The select element

  • Option: “The option element represents an option in a select element.” — Source: WHATWG: The option element

  • Textarea: “The textarea element represents a multiline plain-text edit control for its raw value.” — Source: WHATWG: The textarea element

  • Fieldset: “The fieldset element represents a set of form controls optionally grouped under a common name.” — Source: WHATWG: The fieldset element

  • Legend: “The legend element represents a caption for the rest of the contents of the fieldset element’s parent fieldset element.” — Source: WHATWG: The legend element

  • Reset: input type=reset restores all controls to their initial values without clearing server data. — Source: WHATWG: Reset Button state)

  • Initial value: The default value or selected/checked state declared in markup that reset restores. — Source: WHATWG: Form elements

  • Radio button (input type=radio): "The input element with type radio represents a radio button — a control that allows selection of a single value from a set." — Source: WHATWG: Radio Button state)

  • Select multiple: "The select element with multiple attribute allows multiple options to be selected." — Source: WHATWG: Select element

  • Optgroup: "The optgroup element represents a group of option elements with a common label." — Source: WHATWG: Optgroup element

Mental model: label the question and every answer

A radio-button set has two jobs to do. The legend states the question shared by the set, while each individual label identifies one possible answer. The shared name makes the radios mutually exclusive; their distinct IDs give the labels unambiguous targets.

Change the code, then look at the preview. The preview runs in a sandbox.

Run the code to see the preview.

The preview appears here.

With Email selected, the form contributes contact-method=email. If each radio had a different name, the browser would treat them as separate groups and permit both choices. That would no longer match a question that expects exactly one answer.

Control choices

Reach for a checkbox when the user is making an independent yes/no choice:

Change the code, then look at the preview. The preview runs in a sandbox.

Run the code to see the preview.

The preview appears here.

An unchecked checkbox contributes no entry at all. The server has to interpret that absence correctly; whether to add a hidden field is an application-specific decision. For several independent interests, give the checkboxes the same name and different values when the server expects multiple entries.

Use select when the user must choose from a constrained list:

Change the code, then look at the preview. The preview runs in a sandbox.

Run the code to see the preview.

The preview appears here.

The text inside an option is for the user; its value is what the server receives. If value is omitted, the option text is submitted instead. A native select is usually the better starting point than a custom scripted replacement.

For text that may span multiple lines, use textarea:

Change the code, then look at the preview. The preview runs in a sandbox.

Run the code to see the preview.

The preview appears here.

A textarea's initial value is the text between its opening and closing tags, not a value attribute. Whitespace placed there can become part of that initial content. rows and cols are intrinsic sizing hints; CSS will control the eventual layout.

type="date" gives the browser enough information to provide a localized date interface while submitting a normalized date string such as 2026-09-01. The visible widget differs across browsers. type="number" is appropriate for quantities where numeric ranges and stepping make sense. It is the wrong model for phone numbers, postal codes, card numbers, and other identifiers that can contain non-numeric characters or leading zeros.

More native controls and input hints

Before writing a custom JavaScript widget, check whether HTML already provides a control with the behavior you need. Native behavior usually brings keyboard and assistive-technology support with it.

Group long option lists with optgroup

Change the code, then look at the preview. The preview runs in a sandbox.

Run the code to see the preview.

The preview appears here.

Offer suggestions with datalist

Change the code, then look at the preview. The preview runs in a sandbox.

Run the code to see the preview.

The preview appears here.

Unlike select, a datalist normally suggests values without restricting the user to them. If only the listed values are valid, add an appropriate validation rule rather than assuming the suggestions enforce that restriction.

Hint the mobile keyboard with inputmode

inputmode is only a keyboard hint. It does not validate the submitted value:

Change the code, then look at the preview. The preview runs in a sandbox.

Run the code to see the preview.

The preview appears here.

Start with the semantic input type. Add inputmode when the keyboard you want differs from the one that type alone would suggest.

The multiple attribute is supported by particular controls, including email and file inputs. Its behavior is part of each control's contract, so do not assume that it has one universal meaning across all input types.

The idea (continued)

Common mistakes and debugging

  • Different radio names: they stop behaving as one group.

  • Same ID repeated: labels target ambiguously; IDs remain unique.

  • No fieldset/legend: the shared question may be lost.

  • Checkbox assumed to submit false: unchecked means absent.

  • Phone as number: use type="tel" or text; phone numbers are identifiers.

  • Textarea value attribute: initial content belongs between tags.

  • Empty first option used as a label without guidance: give the select a real label; validation is covered in lesson 010.

  • Reset beside submit: omit unless there is proven value.

  • Prechecked consent: require an intentional user choice.

Accessibility, security, and performance

Native grouping communicates relationships and supplies keyboard interaction. Keep legends concise and phrased as the shared question; make every option understandable through its label. Layout alone should not be carrying that meaning. Date controls look different across user agents, so when an exact format matters, provide visible format guidance and validate the value on the server.

Treat every submitted value as untrusted, including select options and hidden or default values that a user can change. Collect only what you need, use HTTPS, validate and authorize on the server, and encode data when displaying it. Contact forms also need spam and rate controls, but those controls should not become inaccessible puzzles. Native controls are generally both efficient and robust; custom selects often bring large scripts and accessibility defects along with them.

More input types and output

Specialized inputs and calculated results

Several input types give the browser enough information to show the right control: color for a color picker, range for a slider, time for a time of day, url for a web address, and hidden for a value that is submitted but not shown.

A hidden input is visible in the source and in the request, so never use it for secrets or for values the server must trust. output shows the result of a calculation. It is for results, not for values the user types.

Change the code, then look at the preview. The preview runs in a sandbox.

Run the code to see the preview.

The preview appears here.

Worked example

Guided example: extend registration

Replace the form with this version:

Change the code, then look at the preview. The preview runs in a sandbox.

Run the code to see the preview.

The preview appears here.

Select Phone, both interests, experience 1, and a date. Trace the resulting entries. The radio contributes one value, the repeated interest name contributes two, and the number and date contribute text representations. The browser does not turn form data into strongly typed values on the server's behalf.

Tab through the groups as well. In a native radio group, arrow keys select radios; Space toggles a focused checkbox. Exact details can vary by browser and platform, which is a reason to test native behavior before replacing it with a custom control.

Intermediate example: complete contact form

Change the code, then look at the preview. The preview runs in a sandbox.

Run the code to see the preview.

The preview appears here.

The button describes the action instead of using the generic word “Submit.” A reset button is best left out by default: it is easy to activate accidentally and discard entered work. When a real reset requirement exists, use <button type="reset">Reset form</button>, label it clearly, keep it away from submit, and remember what it does. It restores the controls to their markup defaults; it does not undo changes already submitted to the server.

Advanced optional extension: defaults and multiple selection

checked establishes the initial state of a radio or checkbox, and selected establishes the initial option. Defaults should help the user, not quietly make a consent decision for them. In particular, never precheck optional marketing consent.

<select multiple> allows several options to be selected, but the interaction can be difficult to discover and operate, especially when instructions do not account for platform differences. For a short list, a checkbox group is often clearer. If you choose a multiple select, label it, explain the interaction as far as possible without relying on one platform, test keyboard, touch, and assistive-technology use, and make sure the server accepts repeated values.

Try it

Tiered exercises

Level 1: choices

Build one radio group for contact method and one checkbox group for interests. For each possible state, predict the entries that will be submitted.

Level 2: complete

Add select, textarea, date, number, and submit controls to the registration/contact page. Deliberately leave out reset.

Level 3: evaluate

Keyboard-test both groups, inspect the submitted entries, explain unchecked behavior, and compare checkboxes with a multiple select for three interests.

Complete solutions

    Try it with your own example

    Custom-order forms are a useful place to see these controls working together: each one represents a real decision Rina's customers make. Write this form yourself before looking at the answer.

    Rina wants a "custom cake" order form. A customer picks exactly one size (radio), any number of add-ins (checkboxes), and a pickup date. Try writing it first, then compare:

    Change the code, then look at the preview. The preview runs in a sandbox.

    Run the code to see the preview.

    The preview appears here.

    Now trace the submission yourself for a customer who picks the 8-inch size and both add-ins: you should get size=8-inch, addin=chocolate-shavings, and addin=toasted-nuts as three separate entries under the repeated addin name, not one combined value. If the customer picks no add-ins, the addin key is simply absent from the request. That is the same "unchecked means missing" behavior described for checkboxes earlier in this lesson.

    Further reading: MDN — Other form controls covers select, date, and number controls with more browser-by-browser rendering notes.

    Check yourself

    Recap and exit questions

    Radio buttons represent one choice, checkboxes independent choices, select constrained options, textarea multiline text, and date/number specialized values. Fieldset and legend preserve group meaning.

    1. What makes radio buttons mutually exclusive?

    2. What does an unchecked checkbox submit?

    3. Where is a textarea's initial value written?

    4. Why is a phone number not type="number"?

    5. Why are reset buttons usually omitted?

    What makes radio buttons in one group mutually exclusive?

    Choose an answer, then check it.

    A checkbox is unchecked. What does the form submit for it?

    Choose an answer, then check it.

    What’s next

    The idea to take with you

    Radio buttons represent one choice, checkboxes independent choices, select constrained options, textarea multiline text, and date/number specialized values. Fieldset and legend preserve group meaning.

    A small marker for this page. Sign in to save your progress.

    Your own words, in the margin.

    — / 250

    Loading your note…