Skip to content
engineering workbench.by StudyPort Academy
Learning path
Foundations18 minLESSON 03 / 16

Forms and validation

A form is a semantic element with a job: collect values and send them. Each control needs a label a person can see. The browser can check the type before the value leaves the page.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

The logs are the input type, the required state, the label text, and the button text. Those facts live in the HTML. The script only repeated them.

+Go deeper

The label is the name

A label with a for attribute points at the input id. Clicking the label focuses the field. A placeholder is a hint that disappears. It is not the name of the field. The here is the label, the input, and the button.

Types are checks

The type attribute changes what the control does. email looks for an @ sign. number keeps the field to digits and shows stepper buttons. date gives the browser date picker. textarea is a multi-line message, and select is a list of choices. Using the right control does that work without a script. A plain text field can be checked later by script, and the person loses the built-in hint.

Required is visible

The required attribute blocks an empty submit and exposes a required state. Say that the field is required in the label too, so the rule is not only a browser bubble.

What the input type changes
TypeWhat the browser doesWhat you no longer write by hand
emailAsks for an @ shapeA custom pattern for addresses
numberDigits, with stepper buttonsA script that rejects letters
dateA native date pickerA calendar written from scratch
textPlain text, no shape checkThe default when no special type fits

The browser is not the vault

Anyone can remove required in the browser tools and submit anyway. The server, or the place that stores the value, repeats the rule. The browser check is there so an honest mistake is caught early.

The button submits

A button inside the form submits it. type=button does not. A div that listens for clicks is not in the form's submit path. Use a button, then read the fields.

04

Guided challenge

Forms and validation

The input type is email in the HTML. The script still logs text. Read the type attribute from the input.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

email
required
Email
Send

Hints 0/2

    Use all hints or make 3 failed runs (0/3).

    05

    Free sandbox

    Edit the HTML and CSS. The JavaScript tab can read the result. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01What should name a field?
    02Why repeat a required rule on the server?

    Ready to call this one understood?

    Mark this lesson complete to earn 100 XP. You can always revisit it.

    Sign in to save progress
    Next lesson