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.
Your console output appears here.
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.
| Type | What the browser does | What you no longer write by hand |
|---|---|---|
| Asks for an @ shape | A custom pattern for addresses | |
| number | Digits, with stepper buttons | A script that rejects letters |
| date | A native date picker | A calendar written from scratch |
| text | Plain text, no shape check | The 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.
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.
Your console output appears here.
EXPECTED CONSOLE OUTPUT
email required Email Send
Hints 0/2
Use all hints or make 3 failed runs (0/3).
Free sandbox
Edit the HTML and CSS. The JavaScript tab can read the result. This draft is saved separately.
Your console output appears here.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 100 XP. You can always revisit it.