HTML Forms Explained: Labels, Input Types and Built-in Validation
What is an HTML form?
An HTML form is a <form> element that groups controls such as text inputs, checkboxes, menus and buttons so a visitor can enter data and send it to a server. The action attribute sets where the data goes, method sets how it is sent, and each control's name becomes a key in the submitted data.
Key form terminology
A form is made of controls, the elements a visitor interacts with, plus the <form> wrapper that decides where and how their values are sent. Only controls with a name attribute are included in the submission, a detail that catches many beginners out.
| Element or attribute | Purpose |
|---|---|
<form> | Wraps the controls and defines action and method |
<label> | Gives a control a visible, accessible name |
<input> | Single-line control whose type sets its behaviour |
<textarea> | Multi-line text |
<select> and <option> | A list of choices |
<fieldset> and <legend> | Group related controls under a caption |
<button> | Submits the form by default, or runs script with type="button" |
name | The key the value is sent under |
How a form submission works: method and action
When a visitor presses a submit button, the browser checks the built-in validation rules, collects every named, enabled control and sends the values to the URL in action. If action is missing, the form submits to the current page.
- GET appends the data to the URL as a query string, such as
/search/?q=php. Use it for searches and filters that are safe to bookmark, share or repeat. - POST sends the data in the request body. Use it for anything that changes data on the server, such as sign-ups, orders and comments, and for passwords, which should never appear in a URL.
- Forms that upload files need
method="post"andenctype="multipart/form-data", otherwise only the file name is sent.
On the server, PHP reads GET values from $_GET and POST values from $_POST, keyed by each control's name. Our PHP basics guide shows how to read and escape them.
Labels and input types with examples
Every control needs a label. Link the two by matching the label's for to the input's id, or wrap the input inside the label. A linked label is read aloud by screen readers and makes the text clickable, which gives a larger target for checkboxes and radio buttons. Placeholder text is not a label: it disappears as soon as the visitor types and is often low in contrast.
<form action="/register/" method="post">
<label for="full-name">Full name</label>
<input id="full-name" name="full_name" type="text" autocomplete="name" required>
<label for="email">Email</label>
<input id="email" name="email" type="email" autocomplete="email" required>
<label for="phone">Mobile number</label>
<input id="phone" name="phone" type="tel" autocomplete="tel"
pattern="[0-9]{10}" aria-describedby="phone-hint">
<p id="phone-hint">10 digits, no spaces or country code</p>
<label for="age">Age</label>
<input id="age" name="age" type="number" min="16" max="99">
<label for="start">Preferred start date</label>
<input id="start" name="start_date" type="date">
<label><input type="checkbox" name="newsletter" value="yes"> Send me course updates</label>
<button type="submit">Register</button>
</form>The type attribute changes more than appearance. email and url check the format, tel and number show suitable on-screen keyboards on phones, and date offers a native date picker. autocomplete tokens such as name, email and tel let browsers fill in saved details, which speeds up forms and reduces typing errors.
Built-in validation attributes
HTML can check many rules before the form is sent, with no JavaScript. The browser shows a message next to the first invalid field and blocks submission until it is fixed.
| Attribute | Rule it enforces |
|---|---|
required | The field cannot be empty |
minlength / maxlength | Number of characters in text fields |
min / max / step | Range and increments for numbers and dates |
pattern | A regular expression the whole value must match |
type="email" / type="url" | Basic format checks |
Style invalid fields with the :user-invalid pseudo-class, which only matches after the visitor has interacted with a field, unlike :invalid, which turns empty required fields red before anyone has typed. Add novalidate to the form only if you replace the browser checks with your own script.
Client-side validation is a convenience for honest visitors. Anyone can bypass it by editing the page or sending a request directly, so the server must validate every value again. The PHP security guide covers server-side validation and escaping.
Grouping controls with fieldset and legend
A group of radio buttons or checkboxes answers one question, so the question itself needs to be announced. Wrap the group in <fieldset> and put the question in <legend>; screen readers then read the legend before each option.
<fieldset>
<legend>Preferred batch timing</legend>
<label><input type="radio" name="timing" value="morning" required> Morning</label>
<label><input type="radio" name="timing" value="evening"> Evening</label>
<label><input type="radio" name="timing" value="weekend"> Weekend</label>
</fieldset>Radio buttons with the same name form a group where only one can be chosen, and required on one of them makes the whole group required. Give each option a value, or the server receives the unhelpful default on.
Accessible form habits
- Keep labels visible and above or beside their fields, and mark required fields in the label text, not by colour alone.
- Connect hints and error messages to the field with
aria-describedbyso screen readers announce them. - Write error messages that say how to fix the problem, such as "Enter a 10-digit mobile number", not just "Invalid".
- Keep the source order the same as the visual order so keyboard users tab through fields logically.
- Use a real
<button>for submitting, and never remove the focus outline unless you style a clearly visible alternative. - Ask only for the data you need; every extra field is more work for the visitor.
Our accessibility guide explains how to test forms with a keyboard and a screen reader.
Practical exercise: build a course enquiry form
Create enquiry.html using the page structure from the HTML fundamentals guide.
- Add fields for name, email, mobile number, city (a
<select>) and a message (<textarea>withmaxlength="500"). - Add a fieldset of radio buttons asking which subject interests the visitor.
- Make name, email and subject required, and validate the mobile number with
pattern. - Add a hint under the mobile field linked with
aria-describedby. - Style invalid fields with
:user-invalid. - Set
method="get", submit the form and read the query string in the address bar; then switch topostand inspect the request body in the Network panel.
You are done when every field can be reached and completed with the keyboard alone, clicking each label focuses its field, and the browser blocks an empty or malformed submission. Processing a form like this in PHP is part of SkillNewton's PHP foundation course.
Common mistakes
- Using placeholder text instead of a visible
<label>, so the field loses its name once the visitor starts typing. - Forgetting the
nameattribute, so the control's value is never sent to the server. - Sending passwords or personal data with
method="get", which puts them in the URL, browser history and server logs. - Relying on HTML validation alone and skipping server-side checks.
- Leaving out
enctype="multipart/form-data"on a file upload form. - Placing a plain
<button>inside a form for a script action, which submits the form because the default type issubmit.
Frequently asked questions
Can a form work without JavaScript?
Yes. A form with an action, a method and named controls submits data and runs built-in validation with no script at all. JavaScript is optional, for example to send data in the background with the Fetch API.
What is the difference between disabled and readonly?
A readonly field cannot be edited but its value is still submitted and it can receive focus. A disabled field cannot be focused or edited, and its value is not submitted at all.
How do I change the browser's validation message?
Call setCustomValidity() on the input from JavaScript with your own text, and clear it with an empty string once the value is valid. Many sites instead show their own message element linked with aria-describedby.
Should a form have one column or several?
A single column is usually easier to scan and complete, especially on phones, because the visitor moves straight down. Short related fields, such as city and PIN code, can sit side by side on wide screens.