T1 HTML · Lesson 5
Forms
Where a form's answers go and how; input boxes and their types; checkboxes, radio buttons, drop-down lists, suggestions and text areas; labels, groups and buttons; and the browser's checks, with why the server must check again.
- write a form, say where its answers go and how, and read exactly what it sends
- choose the right input for each answer: text, password, email, number or a slider
- offer choices with checkboxes, radio buttons, drop-down lists, suggestions and text areas
- give every box a label, group related boxes, and choose the right kind of button
- make the browser check the answers before sending, and say why the server must check again
The back cover
Every search box, login page and order form on the web is an HTML form. A form is a set of boxes for a visitor to fill in. It also sends the answers to a server. It works like a bank's deposit slip: each box has a printed name, and the filled slip is handed over.
These pages show where a form's answers go, and how. They show the many kinds of box: for text, passwords, emails and numbers, ticks, one choice among many, lists and long answers.
Then come the labels and groups that let everyone use a form, and the checks the browser makes before sending. Those checks help honest visitors; they never replace the server's own checks.
Before you start
The story: Petitions gone wrong
The Town Hall wants citizens to send petitions from the towers instead of queuing at its doors. The clerk shows you their first try: a panel with boxes to fill in and a button marked Go.
It has gone badly. Half the petitions arrive with no names. Some arrive twice. One citizen's secret ration number showed on the Hall's public board for a whole morning. And Wren could not tell which box was for what, because the boxes had no names she could hear.
"Make it work," says the clerk, "and make it safe."
So far every page has talked and the visitor has only read. Forms turn this around: the visitor types, ticks and chooses, and the page sends what they gave to a server. Every search box, login page, order and sign-up on the web is a form. It builds on these ideas; each has a tiny example.
| What you already know | A tiny example | Where it comes back |
|---|---|---|
| A query holds name=value pairs. | ?from=Pune&to=Mumbai, pairs joined with &. | A form sends its answers as exactly such pairs. |
| GET and POST. | GET asks for something; POST sends something to be stored. | A form chooses one of them as its method. |
| HTTPS seals what travels. | The padlock: nobody on the way can read it. | Passwords and card numbers must only ever be sent over HTTPS. |
| Empty elements. | <img> and <br> have no closing tag. | The input element is empty too. |
| Attributes switched on by being written. | controls on a video, reversed on a list. | required, checked and selected work the same way. |
| id names one element. | <h2 id="offers">. | A label is joined to its box through the box's id. |
| Screen readers read the page aloud. | They read alt text and table headings. | They read each box's label, so every box needs one. |
| A paper form. | A bank's deposit slip: boxes, each with a printed name, handed in at the counter. | An HTML form is the same thing on a screen. |
| The browser checks what it can. | It moves text out of a broken table by itself. | It also checks a form's answers before sending them. |
By the end of this lesson you will be able to:
- write a form, say where its answers go and how, and read exactly what it sends;
- choose the right input for each answer: text, password, email, number or a slider;
- offer choices with checkboxes, radio buttons, drop-down lists, suggestions and text areas;
- give every box a label, group related boxes, and choose the right kind of button;
- make the browser check the answers before sending, and say why the server must check again.
In one line
A form collects the visitor's answers and sends them as name=value pairs; you need lesson 1's queries, GET and POST, and HTTPS, and lesson 2's attributes and ids.
The form element: where the answers go
At a glance
A form element holds the boxes. Its action attribute is the address the answers are sent to, and its method says how. Each box's name becomes the name in a name=value pair, so a box without a name is not sent at all.
With the method GET, the pairs travel in the address, as a query that can be bookmarked and shared: the right choice for a search. With POST, they travel inside the request, out of the address bar and history: the right choice for logins, orders and payments.
Take a guess: Which method should a login form use, GET or POST?
Think of a deposit slip at a bank. It has boxes, and each box has a printed name: Account number, Amount, Date. You fill the boxes and hand the slip in at a counter. The clerk reads each box by its name. An HTML form works the same way, and each part of the slip has its own piece of HTML.
A form element holding the boxes a visitor fills in. Its action attribute says where the answers are sent; its method attribute says how they are sent.
<form action="https://www.google.com/search" method="get">
<input name="q" value="masala chai recipe">
<button>Search</button>
</form>
Checked in Chrome
- pressing Search would send q=masala+chai+recipe
- the method is GET
- the answers go to Google's search address
This is a real search form. The input element is the box. Its name is q, and its starting value is masala chai recipe. The button sends the form.
When the visitor presses Search, the browser builds the pair q=masala+chai+recipe from the box's name and what is in it. A space becomes a +. With the GET method, it adds the pair to the action's address as a query, exactly as in lesson 1, and goes there:
https://www.google.com/search?q=masala+chai+recipe
You can try it: type that address into your browser's address bar, and Google searches for masala chai recipe. That is all a search form does.
In this lesson's previews, nothing is sent
Pressing a button in a preview here does nothing: the previews are not allowed to send forms anywhere. Instead, each check reads what the form would send, built from the same pairs the browser sends. That is how you can see them.
The name decides what is sent
The clerk at the bank reads each box by its printed name. The server does the same: it reads each answer by its name. So a box with no name is not sent at all, whatever the visitor types in it.
<form action="/search" method="get">
<input name="from" value="Pune">
<input name="to" value="Mumbai">
<input value="this box has no name">
</form>
Checked in Chrome
- only the two named boxes are sent: from=Pune&to=Mumbai
The third box shows on the page, and the visitor can type in it, but its text never leaves the browser. Every box whose answer matters needs a name.
GET or POST?
| GET | POST | |
|---|---|---|
| where the answers travel | in the address, as a query | inside the request, out of sight |
| seen in the address bar and history | yes | no |
| can be bookmarked or shared | yes: the address holds everything | no |
| use it for | searches and filters: asking for something | logins, orders, payments, sign-ups: sending something to be stored |
A search is a question, so GET suits it: the address of a search result can be bookmarked and shared on WhatsApp. A login sends a password, which must never sit in an address bar or a history list, so it uses POST. If a form leaves out the method, the browser uses GET.
A common mistake
A login form with method="get", or with no method at all. The password then appears in the address bar, in the history and in the server's logs. Any form with a password, or one that orders, pays or changes something, uses method="post".
Where do the answers go after that? To a program on the server, which reads them and replies with a page. This lesson builds the form; the Node.js step writes the program that receives it.
Worked example 1
A railway site wants a search form that sends the starting station and the destination to /trains/search, so that the results can be bookmarked. Write it with Pune and Nagpur already filled in, and say what it sends.
What we have: two answers, an address, and the wish to bookmark the results
What we need: a form with the right action, method and names
The plan: bookmarking means the answers must be in the address, so GET; each box gets a name the server will read
<form action="/trains/search" method="get">
<input name="from" value="Pune">
<input name="to" value="Nagpur">
<button>Find trains</button>
</form>
Checked in Chrome
- it would send from=Pune&to=Nagpur
- GET, so the results can be bookmarked
A GET form to /trains/search with two named boxes; it sends from=Pune&to=Nagpur.
Check: the check builds the pairs exactly as the browser would, and they match.
A path that fails: Leaving out the names because the boxes "look fine". The form would then send nothing at all, and the search would have no stations.
Worked example 2
Choose GET or POST for three forms on a shopping site: a filter that shows phones under 15,000 rupees, a login with a one-time password (OTP), and a newsletter sign-up that saves an email address.
What we have: three forms, each either asking for something or sending something
What we need: the method for each, with its reason
The plan: for each form, ask: is it a question (GET), or does it send something private or to be stored (POST)?
- The filter only asks for a list of phones. Its address, such as /phones?max=15000, is worth bookmarking and sharing. So GET.
- The OTP login sends a secret code. It must never sit in the address bar or the history. So POST.
- The sign-up stores an email address on the server, and an address is private. So POST.
The filter: GET. The OTP login: POST. The newsletter sign-up: POST.
Check: every form that sends a secret or stores something used POST; the only GET is the one whose address is worth sharing.
A path that fails: Choosing GET for the sign-up "because it is only one box". The size of the form does not matter; what matters is that the email address would then sit in the address bar and the server's logs.
When you are stuck: forms
Ask three questions: where do the answers go (action), how (method), and what is each box called (name)?
If an answer never reaches the server, check that its box has a name.
Asking for something: GET. Sending something private or to be stored: POST.
Your turn
What does a form's action attribute hold?Tap to reveal
- Think of the counter at the bank.
The address the answers are sent to.
A box has no name attribute. Is what the visitor types in it sent?Tap to reveal
- The server reads answers by their names.
No: a box without a name is not sent.
Which method should a login form use, and why?Tap to reveal
- Where would a password travel with GET?
method="post", so that the password never appears in the address bar, the history or the logs.
Did you know?
Forms arrived with HTML 2.0
The first web pages could show things but not ask anything. Forms became part of the standard with HTML 2.0, RFC 1866, published in November 1995 by Tim Berners-Lee and Dan Connolly. It even defined two levels of HTML: level 1 is everything except the features that need the FORM element, and level 2, the default, includes form processing.
Why it fits here: The form element of this topic is what turned the web from pages to read into pages that can take answers.
How long an address may be
With GET, every answer travels inside the address, so an address can grow long. The HTTP standard of June 2022, RFC 9110, recommends that every sender and receiver support addresses of at least 8000 octets, which means 8000 bytes. A search fits easily; a long essay typed into a form does not belong in an address at all.
Why it fits here: This is one more reason why this topic sends long or private answers with POST and keeps GET for short searches.
Source: RFC 9110: HTTP Semantics
In one line
A form sends each named box as a name=value pair to its action address, and a box with no name is not sent. GET puts the pairs in the address (for searches); POST sends them out of sight (for logins and orders).
Input boxes and their types
At a glance
An input element is a box, and its type attribute decides what kind. A text box takes words. A password box hides the answer on the screen only; the form still sends it as plain text, and HTTPS keeps it safe on the way. An email box checks that the answer has the shape of an email address.
A number box takes only numbers, between its min and max. A range is a slider, for when a rough amount is enough. A value is a real answer, already filled in, while a placeholder is only a grey hint that disappears.
Take a guess: Does a password box send the password secretly, or only hide it on the screen?
A paper form uses different boxes for different answers: a row of small squares for a PIN code, a long line for an address, a box to tick. The input element is HTML's box, and one attribute, type, decides which kind of box it is.
An input element: one box the visitor fills in. Like img, it is an empty element, with no closing tag. Its type says what kind of answer it takes; with no type, it is a plain text box.
| type | The box | Good for |
|---|---|---|
type="text" (the default) | one line of any text | a name, a city, a search |
type="password" | one line, shown as dots | a password or a PIN |
type="email" | one line that must look like an email address | an email address |
type="number" | a number, with small up and down arrows | a quantity, an age |
type="range" | a slider between two ends | a rough amount: volume, spice level |
Text and password
<form>
<input type="text" name="user" value="asha.k">
<input type="password" name="pin" value="4821">
</form>
Checked in Chrome
- the PIN box shows dots, not digits
- but the form sends the PIN as plain text: pin=4821
Look at the second check. A password box only hides the answer on the screen, from someone looking over your shoulder. The form still sends it as plain text, pin=4821. What keeps it safe on the way is HTTPS, the sealed envelope of lesson 1, and the POST method, which keeps it out of the address bar.
What protects a password
Three things, each with its own job: type="password" hides it on the screen; method="post" keeps it out of the address and history; HTTPS seals it on its way. A login form needs all three.
Email: a box that checks
A box of type email looks like a text box, but the browser checks that its answer looks like an email address. On a phone, it also shows a keyboard with the @ key ready.
<input type="email" value="asha@ashastores.example">
<input type="email" value="asha">
Checked in Chrome
- asha@ashastores.example passes
- asha fails: there is no @
The check calls checkValidity(), which asks the browser: "would you accept this answer?" The browser checks only the shape, a name, an @ and a domain. It cannot know whether the address is real; only an email sent to it can tell.
Number and range
A box of type number takes only a number. Its attributes min and max set the smallest and largest answers allowed. A range is a slider over the same kind of span, for when a rough amount is enough.
<form>
<input type="number" name="cups" min="1" max="10" value="2">
<input type="range" name="spice" min="0" max="10">
</form>
Checked in Chrome
- cups holds the number 2
- the slider, given no value, starts in the middle: 5
- the form sends cups=2&spice=5
The slider was given no value, so the browser put it in the middle of 0 to 10, at 5. A form always sends text, even from a number box: cups=2 travels as the characters 2. The server's program turns it back into a number.
value and placeholder
Two attributes put words in a box, and they are often confused. The value is a real starting answer: it is sent if the visitor leaves it alone. A placeholder is only a grey hint shown while the box is empty, such as "for example 411001". It vanishes when the visitor types, and it is never sent.
<form>
<input name="city" value="Pune">
<input name="pin" placeholder="for example 411001">
</form>
Checked in Chrome
- city sends Pune; the pin box sends nothing, because a placeholder is not an answer
A placeholder is not a label
A box whose only explanation is its placeholder loses it the moment the visitor starts typing, and many screen readers do not read placeholders reliably. Every box needs a real label, which the labels section shows.
Worked example 3
A chai stall's order form needs the number of cups, from 1 to 20, starting at 1. Write the box, and say what the form sends if the visitor changes nothing.
What we have: a whole number with a smallest and a largest value, and a starting value
What we need: an input of the right type with the right attributes
The plan: a number box; min and max for the limits; value for the start; a name so it is sent
<form>
<input type="number" name="cups" min="1" max="20" value="1">
</form>
Checked in Chrome
- it sends cups=1
- from 1 to 20
<input type="number" name="cups" min="1" max="20" value="1">; it sends cups=1.
Check: the check builds what the form would send and finds cups=1.
A path that fails: Writing the starting 1 as a placeholder. The box then looks the same, but it sends cups= with nothing after it.
When you are stuck: input boxes
Ask what kind of answer the box takes: any text, a secret, an email, a number, or a rough amount. That is its type.
A starting answer that should be sent is a value; a hint that should not is a placeholder.
Every box that must be sent needs a name.
Your turn
Does type="password" stop the password being sent as plain text?Tap to reveal
- Look at what the form sent in the password block.
No: it only hides it on the screen. HTTPS and POST protect it on its way.
A range box with min="0" and max="100" has no value. Where does the slider start?Tap to reveal
- The middle of 0 to 100.
At 50.
Which is sent: a value or a placeholder?Tap to reveal
- One is an answer, one is a hint.
The value; a placeholder is never sent.
Did you know?
The first computer password
Computer passwords are older than the web by decades. Nearly all the computer historians Wired asked in 2012 said that the first password must have come from MIT's Compatible Time-Sharing System, CTSS, probably in the mid-1960s. Fernando Corbató, who led the project, was a little reluctant to take the credit, and later said the method had spread and become unmanageable. On CTSS, typing PASSWORD switched off the printing, so the answer stayed hidden.
Why it fits here: The password box of this topic does what CTSS did: it keeps the answer off the screen, and that alone does not make it secret.
Sources: Wired: The World's First Computer Password? It Was Useless Too; Wikipedia: Fernando J. Corbató; Wikipedia: Password
An email check that breaks the rules on purpose
The official rules for email addresses, RFC 5322, allow strange forms such as comments and quoted strings. The HTML standard's check for an email box does not follow them, and says so openly. It calls its own rule a willful violation of RFC 5322. The old syntax, it explains, is too strict before the @, too vague after it and too lax overall.
Why it fits here: The email box of this topic checks a shape that real people's addresses have, not every form the old rules allow.
In one line
An input is an empty element whose type picks the box: text, password (hidden on screen only), email (checked for its shape), number (min and max) or range (a slider). A value is a real answer; a placeholder is only a hint.
Choices: ticks, one of many, lists and long answers
At a glance
Many answers are chosen, not typed. A checkbox is a small square for a yes-or-no answer; any number can be ticked, and an unticked box sends nothing. Radio buttons that share a name let exactly one be chosen, and send one pair.
When there are many choices, a select element shows one line that opens into a list of option elements, and it sends the chosen option's value. A datalist only suggests answers while the visitor types. A textarea holds a long answer of many lines.
Take a guess: Three radio buttons share one name: how many of them can be chosen at once?
Many questions on a form are not typed at all. "Tick all that apply." "Choose one: small, medium, large." "Pick your state from the list." HTML has a box for each of these, and each sends its answer in its own way.
Checkboxes: tick any number
An input of type="checkbox": a small square the visitor ticks or leaves empty. Each one is a yes-or-no answer on its own.
<form>
<input type="checkbox" name="ginger" value="yes" checked> Ginger
<input type="checkbox" name="elaichi" value="yes"> Elaichi
<input type="checkbox" name="sugar" checked> Sugar
</form>
Checked in Chrome
- only the ticked boxes are sent: ginger=yes&sugar=on
Three things to see. The attribute checked ticks a box from the start. An unticked box sends nothing at all: elaichi is missing from what is sent. And a ticked box with no value sends the word on, as sugar did; give each box a value so the server gets a clear answer.
Radio buttons: exactly one
Inputs of type="radio" that share one name. They form a group, and only one in the group can be chosen. Choosing another unchooses the first, like the station buttons on an old car radio, which is where the name comes from.
<form>
<input type="radio" name="size" value="small"> Small
<input type="radio" name="size" value="medium" checked> Medium
<input type="radio" name="size" value="large"> Large
</form>
Checked in Chrome
- three buttons in one group, all named size
- exactly one is chosen
- the group sends one pair: size=medium
The shared name is what makes the group: three buttons, one name, one answer. The form sends the group's name with the value of the chosen button. A common mistake is giving each button its own name; each then becomes a group of one, and the visitor can choose all three.
| Checkbox | Radio | |
|---|---|---|
| how many can be chosen | any number, or none | exactly one in each group |
| what joins them | nothing: each box stands alone | a shared name |
| what is sent | one pair for each ticked box | one pair for the group |
| the paper form says | "Tick all that apply" | "Choose one" |
A drop-down list: select and option
When there are many choices, such as the states of India, a row of radio buttons would fill the screen. A select element shows one line that opens into a list. Each choice is an option element inside it.
<form>
<select name="state">
<option value="MH">Maharashtra</option>
<option value="KA" selected>Karnataka</option>
<option value="TN">Tamil Nadu</option>
</select>
</form>
Checked in Chrome
- Karnataka is chosen: its value is KA
- the form sends state=KA, the value, not the words
The visitor sees the words, Karnataka; the form sends the option's value, KA, which is shorter and never changes if the words are reworded. The attribute selected chooses an option from the start, as checked does for a box. With no selected, the first option is chosen.
Suggestions while typing: datalist
Sometimes you want to suggest answers but still let the visitor type anything. A datalist holds the suggestions, and the input names it in its list attribute, by the datalist's id.
<form>
<input name="city" list="cities" value="Goa">
<datalist id="cities">
<option value="Pune">
<option value="Nagpur">
<option value="Nashik">
</datalist>
</form>
Checked in Chrome
- the box offers three suggestions
- but Goa, which is not among them, is still sent
As the visitor types, the browser shows the matching suggestions below the box. The datalist itself is never shown on the page. Unlike a select, it does not limit the answer: Goa was not suggested, and it is sent all the same.
A long answer: textarea
An input holds one line. For an address or a message, a textarea holds many. Unlike input, it has a closing tag: the text between its tags is its starting answer. Its rows attribute sets how many lines tall it shows.
<form>
<textarea name="address" rows="3">Shop 4, MG Road
Pune 411001</textarea>
</form>
Checked in Chrome
- the textarea holds both lines
- it sends them with the comma and the line break written in code
Look at what is sent. Spaces become +, the comma becomes %2C, and the line break becomes %0A. This is percent-encoding: characters that could be mistaken for parts of an address are written as % and a code. The server turns them back, so the address arrives exactly as typed.
One small difference: when a form is really sent, the browser writes each line break as %0D%0A, two codes instead of one, because that is the line ending internet standards use. The check here shows the single %0A. The server reads both as one line break.
Worked example 4
A tiffin service's form asks: the meal (exactly one of Veg or Non-veg, Veg chosen first), extras (any of Curd and Papad, none ticked), and the area from a list (Kothrud, Baner, Aundh). Write it so that it sends meal=veg&area=baner when Baner is chosen.
What we have: one choose-one question, one tick-any question, one pick-from-a-list question
What we need: the right box for each, with names and values that give the pairs asked for
The plan: radios sharing the name meal; checkboxes for the extras; a select named area with lower-case values
<form>
<input type="radio" name="meal" value="veg" checked> Veg
<input type="radio" name="meal" value="nonveg"> Non-veg
<input type="checkbox" name="curd" value="yes"> Curd
<input type="checkbox" name="papad" value="yes"> Papad
<select name="area">
<option value="kothrud">Kothrud</option>
<option value="baner" selected>Baner</option>
<option value="aundh">Aundh</option>
</select>
</form>
Checked in Chrome
- it sends meal=veg&area=baner
- both meal buttons share one name
Two radios named meal, two checkboxes, and a select named area; with nothing ticked, the extras send nothing, so the form sends meal=veg&area=baner.
Check: the extras are missing from what is sent because neither box is ticked, which is how checkboxes behave.
A path that fails: Naming the radios meal-veg and meal-nonveg. They would then be two separate groups, and a visitor could choose both.
When you are stuck: choices
Ask: can the visitor choose several (checkboxes), exactly one of a few (radios sharing a name), or one of many (select)?
Suggestions that still allow any answer: datalist. Many lines: textarea.
Give every choice a value: it is what the server receives.
Your turn
What does an unticked checkbox send?Tap to reveal
- Look at elaichi in the checkbox block.
Nothing at all: it is left out.
What makes three radio buttons one group?Tap to reveal
- One attribute, the same in all three.
A shared name.
An option reads Tamil Nadu and has value="TN". Which does the form send?Tap to reveal
- The visitor sees one; the server gets the other.
The value, TN.
Did you know?
Radio buttons are named after radios
Radio buttons got their name from real radios. Older radios had a row of buttons for preset stations: pressing one pushed it in and made the others pop out, so only one could be pressed at a time. MDN describes the HTML version the same way: radio buttons look and work like the push buttons on old-fashioned radios.
Why it fits here: Exactly one choice at a time, the rule of this topic's radio buttons, is the rule of those old station buttons.
Sources: Wikipedia: Radio button; MDN: input type radio
More choices, slower decisions
In 1952 the psychologist W. E. Hick measured how long people take to choose. The more choices they had, the longer they took. Designers call this Hick's law: the time it takes to make a decision increases with the number and complexity of choices. The usual advice is to show fewer choices at once, or to split a long task into smaller steps.
Why it fits here: This is why this topic puts a long set of choices into a select or a datalist, and keeps radio buttons for a few.
Sources: Wikipedia: Hick's law; Laws of UX: Hick's Law
In one line
Checkboxes send a pair for each ticked box, and none for unticked ones. Radios sharing a name send one pair. A select sends the chosen option's value, a datalist only suggests, and a textarea holds many lines.
Labels, groups and buttons
At a glance
A label is the name printed beside a box. Its for attribute matches the box's id, or the label is wrapped around the box. Either way, clicking the label puts the cursor in the box, and a screen reader reads the label with it. A fieldset groups related boxes, and its legend asks the group's question.
A button's type decides what it does. A button with no type inside a form sends the form, so any other button needs type="button". Labels, groups and clear buttons make a form that everyone can use.
Take a guess: What does clicking on a box's label do?
Every box on a paper form has its name printed beside it: Name, Mobile, Address. Without those words, a box is a blank square that could mean anything. On a web page, the printed name is the label element, and it does more than paper can.
A label element holding the words that name one box. Its for attribute holds the box's id, which joins the two.
<form>
<label for="mobile">Mobile number</label>
<input id="mobile" name="mobile" type="tel">
</form>
Checked in Chrome
- the box has its label
- and the label reads Mobile number
The label's for is mobile; the box's id is mobile. That match is the whole join. (The box's type, tel, shows a keypad of digits on a phone.) Now three good things happen:
- a screen reader, reaching the box, says "Mobile number, edit text", so a blind visitor knows what to type;
- clicking or tapping the words puts the cursor in the box, a bigger target for a finger;
- for a checkbox, tapping its words ticks it, which the next block shows.
<form>
<input type="checkbox" id="terms" name="terms" value="yes">
<label for="terms">I accept the terms</label>
</form>
Checked in Chrome
- clicking the words ticks the box
- and the ticked box is now sent
The check clicks the label's words, as a visitor's finger would, and the box becomes ticked. A tiny square is hard to hit on a phone; its words are easy.
A second way: the label around the box
Instead of for and id, you can put the box inside the label. The label then names whatever it holds, with no id needed.
<form>
<label><input type="radio" name="pay" value="upi" checked> UPI</label>
<label><input type="radio" name="pay" value="cash"> Cash on delivery</label>
</form>
Checked in Chrome
- each button is named by the label around it
- clicking Cash on delivery chooses it
Groups: fieldset and legend
A group of radio buttons answers one question, such as "How will you pay?". Each button has its own label, UPI or Cash, but the question itself needs a place too. A fieldset element draws a box around the group, and its legend, first inside it, holds the question.
<form>
<fieldset>
<legend>How will you pay?</legend>
<label><input type="radio" name="pay" value="upi" checked> UPI</label>
<label><input type="radio" name="pay" value="card"> Card</label>
</fieldset>
</form>
Checked in Chrome
- the question comes first, as the legend
- both choices sit inside the group
A screen reader now says the question with each choice: "How will you pay? UPI, radio button, 1 of 2". The listener never hears a lone "Card" with no idea what it answers.
Buttons: send, or something else
A button element is a button with words or a picture inside it. Its type decides what it does, and the default holds a trap.
| type | What pressing it does |
|---|---|
type="submit" (the default inside a form) | sends the form |
type="button" | nothing by itself; a script gives it a job, such as showing a password (the JavaScript step) |
type="reset" | puts every box back to its starting answer, wiping what the visitor typed |
<form>
<button>Place order</button>
<button type="button">Show the price list</button>
</form>
Checked in Chrome
- a button with no type sends the form
- type="button" does not
A common mistake
Every button inside a form with no type is a submit button. So a "Show the price list" button written without a type sends the half-filled form the moment it is pressed. Give every button that is not meant to send the form type="button". And think twice before a reset button: one slip of the finger wipes a long form.
A form everyone can use
Putting this section together gives a short list for accessible form design: forms that work for everyone, including people who use screen readers, keyboards or large text.
- every box has a visible label, joined to it; a placeholder is never the only name;
- related choices sit in a fieldset with a legend that asks the question;
- the right type for each box, so phones show the right keyboard;
- required boxes are marked in words, such as "(required)", not only with a colour or a star;
- the button's words say what it does: "Place order", not "Submit".
Worked example 5
A sign-up form has two boxes with only placeholders, Name and Email, and a button with the word Go. Rewrite it so that every box is labelled and the button says what it does.
What we have: two unlabelled boxes and a vague button
What we need: labels joined to the boxes, and clear button words
The plan: give each box an id and a label with a matching for; change the button's words
<form method="post">
<label for="name">Name</label>
<input id="name" name="name">
<label for="email">Email</label>
<input id="email" name="email" type="email">
<button>Create my account</button>
</form>
Checked in Chrome
- every box has its label
- the button says what it does
Two labels joined by for and id, the email box typed, and the button reading Create my account.
Check: the check confirms that every input has exactly one label.
When you are stuck: labels and buttons
Check each label's for against the box's id, letter by letter: they must be the same.
If a form sends itself too early, find the button with no type and give it type="button".
For a group of choices, put the question in a legend.
Your turn
A label has for="pin". What must the box have?Tap to reveal
- The two are joined through one attribute of the box.
id="pin".
What does a button with no type do inside a form?Tap to reveal
- The default holds a trap.
It sends the form: its type is submit.
Which element holds the question of a group of radio buttons?Tap to reveal
- It goes first inside a fieldset.
<legend>.
Did you know?
Half the top sites have boxes with no label
WebAIM's 2026 test of the home pages of the top one million websites found missing form input labels on 51% of them, the third most common accessibility error after low-contrast text and missing alternative text. The report adds that these most common errors have been the same for the last seven years.
Why it fits here: A label for every box, as this topic teaches, fixes one of the commonest faults on the web.
A label makes the target bigger
Fitts's law, from the psychologist Paul Fitts, says that the time to reach a target depends on its size and its distance. The Nielsen Norman Group applies it to screens: a target made of an icon and a label is bigger than the icon alone, so it is quicker to hit. A label joined to its box works the same way, since clicking either one reaches the box.
Why it fits here: Joining a label to its box, as this topic does with for and id, turns the words into part of the target.
Source: Nielsen Norman Group: Fitts's Law and Its Applications in UX
In one line
A label names its box: its for matches the box's id, or it wraps the box. A fieldset's legend asks a group's question. A button with no type sends the form, so other buttons need type="button".
Checking the answers before they are sent
At a glance
The browser can refuse wrong answers before a form is sent. The attribute required means a box must be filled. The attributes min and max limit a number, and minlength and maxlength limit the length of text. The attribute pattern describes the shape an answer must have, written as a regular expression. An email box checks for the shape of an email address.
These checks run in the visitor's own browser, so the visitor controls them and can switch them off. The browser checks to help honest visitors; the server must check every answer again.
Take a guess: If the browser checks a form, does the server still need to check the answers?
At a bank counter, the clerk glances at your slip before taking it: "You have not filled the amount", "An account number has 11 digits, this has 9". It saves a trip to the back office and back. The browser can do the same glance, before a form is sent, if you tell it the rules.
Checking that each answer follows its rules before the form is sent. You write the rules as attributes on the boxes; the browser checks them when the visitor presses the submit button, and stops at the first broken rule with a message.
| Attribute | The rule | Example |
|---|---|---|
required | the box must not be empty | <input name="name" required> |
min, max | the smallest and largest number allowed | <input type="number" min="1" max="20"> |
minlength, maxlength | the fewest and most characters allowed | <input name="pw" minlength="8"> |
pattern | the answer must match a pattern of characters | <input pattern="[0-9]{6}"> |
type="email" | it must look like an email address | <input type="email"> |
Each check below uses checkValidity(), which asks the browser "would you send this?", and validity, which says which rule failed.
required
<form>
<input name="name" required>
</form>
Checked in Chrome
- the box is empty, so the rule is broken: the value is missing
- the browser would refuse to send this form
Like checked and controls, required is switched on by being written. One broken rule anywhere stops the whole form: the browser shows its message at that box and sends nothing.
min and max
<input type="number" name="cups" min="1" max="10" value="12">
Checked in Chrome
- 12 is over the max of 10
- so the answer is refused
pattern: the shape of an answer
A pattern describes the shape an answer must have, in a small language of its own. Two pieces cover a great deal: [0-9] means "one digit", and {6} after it means "exactly 6 of those". So [0-9]{6} reads "exactly six digits", the shape of an Indian PIN code. The whole answer must match, not just a part of it.
<input name="pin" pattern="[0-9]{6}" value="411001">
<input name="pin2" pattern="[0-9]{6}" value="4110">
Checked in Chrome
- 411001, six digits, passes
- 4110, only four digits, does not match
An Indian mobile number is ten digits whose first digit is 6, 7, 8 or 9. In the pattern language that is [6-9][0-9]{9}: one digit from 6 to 9, then nine more digits. The JavaScript step teaches this language in full; it is called a regular expression.
minlength and maxlength
These count characters: minlength="8" asks for at least 8, and maxlength="10" allows at most 10. They behave a little differently from the others, in two ways worth knowing.
- maxlength stops the typing itself: once the box holds 10 characters, further keys do nothing, so the rule can never be broken by typing.
- minlength is checked only on what the visitor typed. A starting value written in the HTML is trusted, even if it is too short.
<input name="pw" minlength="8" value="abc">
Checked in Chrome
- the rule asks for at least 8 characters
- yet abc, written in the HTML, passes: only typed answers are counted
This surprises everyone the first time, which is why it is shown here. If the visitor deletes a letter and types it again, the browser starts counting, and abc is refused.
The browser's check is a courtesy, not a lock
Here is the most important idea of this section. These checks run in the visitor's own browser, so the visitor controls them. Anyone can open DevTools and delete the required attribute, or send answers to the server with no page at all, as the Postman step does.
Check twice
The browser checks to help honest visitors; the server checks to stop everyone else. Every rule written in HTML must be checked again by the server's program, which the Node.js step writes. Never trust an answer just because the page checked it.
Worked example 6
Write a box for an Indian mobile number that must be filled in, and say whether 9876543210 and 5876543210 pass.
What we have: the rule: ten digits, the first from 6 to 9, and not empty
What we need: a box with the right attributes, and the result for each number
The plan: required for not empty; the pattern [6-9][0-9]{9}; type="tel" for the phone's digit keypad
<input type="tel" name="mobile" required pattern="[6-9][0-9]{9}" value="9876543210">
<input type="tel" name="mobile2" required pattern="[6-9][0-9]{9}" value="5876543210">
Checked in Chrome
- 9876543210 passes: it starts with 9 and has ten digits
- 5876543210 fails: it starts with 5
<input type="tel" name="mobile" required pattern="[6-9][0-9]{9}">: 9876543210 passes, 5876543210 does not.
Check: the browser itself judged both numbers in the checks, with the same pattern.
A path that fails: Using type="number" for a mobile number. A number box shows arrows for counting up and down, and it is meant for amounts you add and compare. A mobile number is a code made of digits: nobody adds two of them.
When you are stuck: validation
Say the rule in words first ("six digits", "not empty", "at most 10"), then pick the attribute for it.
If a pattern refuses a good answer, remember that the whole answer must match; a space or a +91 at the start breaks it.
Whatever the page checks, write the same check on the server too.
Your turn
Which attribute makes a box impossible to leave empty?Tap to reveal
- It is written with no value.
required.
What does the pattern [0-9]{6} accept?Tap to reveal
- [0-9] is one digit; {6} repeats it.
Exactly six digits, such as a PIN code.
Why must the server check the answers again?Tap to reveal
- Who controls the visitor's browser?
Because the visitor controls the browser: its checks can be removed or skipped entirely.
Did you know?
Patterns from a mathematician
The pattern attribute uses regular expressions, and their idea is older than personal computers. They began in the 1950s, when the American mathematician Stephen Cole Kleene formalized the concept of a regular language. Ken Thompson and Dennis Ritchie brought them into Unix, and by the late 1970s they were a key feature of Unix tools such as ed, sed and grep.
Why it fits here: The pattern of this topic, such as six digits for a PIN code, is a regular expression of exactly that kind.
Sources: Wikipedia: Regular expression; Russ Cox: Regular Expression Matching Can Be Simple And Fast
Never trust the browser alone
OWASP, a foundation that publishes advice on web security, lists client-only checks among the common pitfalls of input validation. Its advice is to validate on the server even when the browser checks the same fields, because client-side validation is bypassable: it is there for immediate feedback, and not for safety.
Why it fits here: This is the courtesy and the lock of this topic: the browser's check is the courtesy, and the server's is the lock.
In one line
required, min and max, minlength and maxlength, pattern and type="email" let the browser refuse wrong answers before sending; checkValidity() asks whether it would send; the server must always check again.
Where it is used
Almost every time you give a website something, you are filling a form, even when it does not look like one.
| What you learned | Where it is used | A real example |
|---|---|---|
| a GET form | search boxes, filters, sorting | a shopping site's search: the address ends with ?q= and your words, so the results can be shared |
| a POST form | logins, sign-ups, orders, payments | a bank's login page sends the customer ID and password with POST, over HTTPS |
| password, email, tel, number | every sign-up and checkout | a food delivery app's checkout: a tel box for the mobile number brings up the digit keypad |
| checkboxes and radios | choices, settings, consent | a railway booking's "Choose a berth" radios, and its "I accept the terms" checkbox |
| select and datalist | long lists, suggestions | a state drop-down on a delivery address; a city box that suggests as you type |
| textarea | messages, addresses, reviews | a product review box on a shopping site |
| labels and fieldsets | every form that is easy to use | a government form that a screen reader reads question by question |
| validation | every form, in the browser and again on the server | a PIN code box that refuses five digits before anything is sent |
Mistakes and debugging
Forms fail quietly: a missing answer on the server, a button that sends too early, a box a screen reader cannot name. Here are the common slips.
1. A box with no name
<form>
<input id="city" value="Pune">
</form>
Checked in Chrome
- (the slip: the box has an id but no name, so nothing at all is sent)
An id joins a label; a name is what is sent. They are different jobs, and this box has only the first. The fix: <input id="city" name="city" value="Pune">.
2. Radio buttons with different names
<form>
<input type="radio" name="size-small" value="small" checked> Small
<input type="radio" name="size-large" value="large" checked> Large
</form>
Checked in Chrome
- (the slip: two names make two groups, so both are chosen at once)
The fix: one shared name, name="size", for every button of the question.
3. A label that points nowhere
<form>
<label for="email">Email</label>
<input id="e-mail" name="email" type="email">
</form>
Checked in Chrome
- (the slip: for="email" does not match id="e-mail", so the box has no label)
On the screen the word Email sits right beside the box, so it looks fine. But tapping it does nothing, and a screen reader says only "edit text". The fix: make for and id exactly the same.
4. A button that sends too early
A "Check the price" button inside a form, written with no type, is a submit button and sends the form. The fix: type="button" on every button that is not meant to send.
5. A password sent with GET
A login form with no method uses GET, so the password lands in the address bar, the history and the server's logs. The fix: method="post", over HTTPS.
6. Trusting the browser's checks
A server that accepts whatever arrives because "the page has required on it". The fix: the server checks every answer again.
The debugging corner: see what was sent
Open DevTools (F12) and the Network tab, then send the form on a real site. Click the request it made: its Payload tab lists every name and value that left the browser. A missing pair there means a missing name, an unticked box, or a box outside the form.
Quick card
The lesson on one card.
Every element of the lesson
<form action="/orders" method="post">
<label for="name">Name</label>
<input id="name" name="name" required value="Asha">
<label for="mobile">Mobile</label>
<input id="mobile" name="mobile" type="tel" required pattern="[6-9][0-9]{9}" value="9876543210">
<label for="cups">Cups</label>
<input id="cups" name="cups" type="number" min="1" max="20" value="2">
<fieldset>
<legend>Size</legend>
<label><input type="radio" name="size" value="small"> Small</label>
<label><input type="radio" name="size" value="large" checked> Large</label>
</fieldset>
<label><input type="checkbox" name="ginger" value="yes" checked> Ginger</label>
<label for="area">Area</label>
<select id="area" name="area">
<option value="kothrud">Kothrud</option>
<option value="baner">Baner</option>
</select>
<label for="note">Note</label>
<textarea id="note" name="note" rows="2">Less sugar</textarea>
<button>Place order</button>
<button type="button">Show prices</button>
</form>
Checked in Chrome
- it would send all seven answers
- POST, for an order
- every rule passes
- every box has its label
- the second button does not send
| Element or attribute | What it is |
|---|---|
<form action method> | where the answers go; GET (in the address) or POST (out of sight) |
name, value | what each answer is called when sent; the starting answer |
<input type> | text, password, email, tel, number, range, checkbox, radio |
checked, selected | a box ticked or an option chosen from the start |
<select>, <option> | a drop-down list; each choice, with its value |
<datalist> with list | suggestions that still allow any answer |
<textarea> | many lines; its starting text sits between its tags |
<label for>, id | the name of a box, joined through the box's id |
<fieldset>, <legend> | a group of boxes; the group's question |
<button type> | submit (the default: sends), button (does not), reset (wipes) |
required, min, max, minlength, maxlength, pattern | the browser's checks; the server checks again |
The story: A slip that asks well and sends safely
You rebuild the petition slip. Every box gets a name the Hall can read and a label any citizen can hear. Choices become ticks and round buttons, so nobody has to type yes. The secret ration number now travels out of sight, sealed, never in the panel's address. And before anything is sent, the panel checks that every box is filled and every number has the right shape.
The clerk reads the morning's petitions: every one complete, none twice, nothing secret on show. "And the Hall checks them again," you remind her. She writes that down.
Reagents gathered: form, name, label, POST, validation.
Sources
- MDN Web Docs (Prose CC BY-SA 2.5 or later; code samples added since 2010-08-20 CC0 (its licensing page, read 2026-10-26); reused with credit)
- HTML Living Standard (WHATWG) (CC BY 4.0: "Copyright WHATWG (Apple, Google, Mozilla, Microsoft). This work is licensed under a Creative Commons Attribution 4.0 International License" (its acknowledgements page, read 2026-10-26); reused with credit)