An HTML form is the part of a page that collects input from users: login boxes, sign-up pages, search bars, contact forms and feedback forms are all HTML forms. The form gathers what the user types and sends it to a server.
On this page you will build a contact form step by step, then go through each common input type with a short example. You only need a text editor and a browser. You can also paste every example into our online HTML, CSS and JS editor and see the result straight away. If HTML itself is new to you, start with our HTML tutorial for beginners.
The form tag
Every form starts with the <form> tag:
<form action="/contact" method="post">
<!-- fields go here -->
</form>
| Attribute | What it does | Example |
|---|---|---|
action |
The URL the data is sent to | action="/contact" |
method |
How the data is sent: get or post |
method="post" |
- GET puts the data in the URL, like
/search?q=java. Use it for searches and filters. - POST sends the data inside the request body. Use it for logins, sign-ups and anything that saves or changes data.
Our GET vs POST page explains the difference in more detail.
Note: HTML only collects and sends the data. A program on the server, written in Java, Python or Node.js, must receive it and save it or email it.
Build a contact form step by step
Step 1: a text box with a label
<form action="/contact" method="post">
<label for="name">Your name</label>
<input type="text" id="name" name="name">
</form>
Three attributes do three different jobs:
| Attribute | Job |
|---|---|
id="name" |
Lets the label point to this field |
for="name" on the label |
Connects the label to the field with the same id |
name="name" |
The key used when the data is sent to the server |
What the browser shows:
Your name [____________________]
Click the words "Your name" in the browser. The cursor jumps into the box. That shows the label is linked correctly, and it also helps screen readers announce the field.
Step 2: add email and phone
<label for="email">Email</label>
<input type="email" id="email" name="email" required>
<label for="phone">Phone</label>
<input type="tel" id="phone" name="phone" placeholder="98765 43210">
type="email"makes the browser check for a valid email shape, such asravi@example.com, and phones show an@key on the keyboard.type="tel"shows a number pad on phones. It does not check the format by itself.requiredstops the form from being sent while the field is empty.placeholdershows grey hint text inside the empty box. It is not a replacement for a label.
Step 3: a drop-down with select
<label for="course">Course</label>
<select id="course" name="course">
<option value="">-- Choose one --</option>
<option value="java">Java Full Stack</option>
<option value="python">Python Full Stack</option>
<option value="data">Data Analytics</option>
</select>
The value is what gets sent to the server. The text between the tags is what the user sees. If the user picks "Data Analytics", the server receives course=data.
Step 4: a message box with textarea
<label for="message">Message</label>
<textarea id="message" name="message" rows="4" cols="40"></textarea>
A textarea accepts many lines of text. rows and cols set its starting size. Unlike input, it has a closing tag.
Step 5: radio buttons and a checkbox
<p>Preferred batch:</p>
<input type="radio" id="morning" name="batch" value="morning">
<label for="morning">Morning</label>
<input type="radio" id="evening" name="batch" value="evening">
<label for="evening">Evening</label>
<input type="checkbox" id="updates" name="updates" value="yes">
<label for="updates">Send me updates on WhatsApp</label>
- Radio buttons with the same name form a group. The user can choose only one.
- A checkbox is on or off. If it is ticked, the server receives
updates=yes. If not, nothing is sent for it.
Step 6: the submit button
<button type="submit">Send message</button>
Clicking it checks the fields and, if everything is valid, sends the form.
The complete contact form
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Contact Us</title>
</head>
<body>
<h1>Contact us</h1>
<form action="/contact" method="post">
<p>
<label for="name">Your name</label><br>
<input type="text" id="name" name="name" required>
</p>
<p>
<label for="email">Email</label><br>
<input type="email" id="email" name="email" required>
</p>
<p>
<label for="phone">Phone</label><br>
<input type="tel" id="phone" name="phone" placeholder="98765 43210">
</p>
<p>
<label for="course">Course</label><br>
<select id="course" name="course">
<option value="">-- Choose one --</option>
<option value="java">Java Full Stack</option>
<option value="python">Python Full Stack</option>
<option value="data">Data Analytics</option>
</select>
</p>
<fieldset>
<legend>Preferred batch</legend>
<input type="radio" id="morning" name="batch" value="morning" checked>
<label for="morning">Morning</label>
<input type="radio" id="evening" name="batch" value="evening">
<label for="evening">Evening</label>
</fieldset>
<p>
<label for="message">Message</label><br>
<textarea id="message" name="message" rows="4" cols="40"></textarea>
</p>
<p>
<input type="checkbox" id="updates" name="updates" value="yes">
<label for="updates">Send me updates on WhatsApp</label>
</p>
<button type="submit">Send message</button>
</form>
</body>
</html>
What the browser shows (without any CSS):
Contact us
Your name
[____________________]
Email
[____________________]
Phone
[98765 43210_________]
Course
[-- Choose one -- v]
+- Preferred batch --------------+
| (o) Morning ( ) Evening |
+--------------------------------+
Message
[ ]
[ ]
[ ]
[ ]
[ ] Send me updates on WhatsApp
[Send message]
New pieces here:
<fieldset>draws a box around related fields, and<legend>gives the box a title. They are perfect for radio groups.checkedmakes "Morning" selected when the page loads.
If a user fills in "Ravi", "ravi@example.com", picks Java and the evening batch, then submits, the browser sends data like this:
name=Ravi&email=ravi%40example.com&phone=&course=java&batch=evening&message=
The @ is encoded as %40. The unticked checkbox is not sent at all. The form looks plain now; our CSS tutorial shows how to style it, and the CSS box model explains the padding, borders and margins you will add to the fields.
Input types in HTML with examples
The type attribute changes what kind of field <input> creates.
| Type | What the user gets | Example |
|---|---|---|
text |
A single-line text box (the default) | <input type="text" name="city"> |
email |
Text box that checks for an email shape | <input type="email" name="email"> |
password |
Text box that hides what you type | <input type="password" name="pwd"> |
number |
Number box with up and down arrows | <input type="number" name="age" min="17" max="30"> |
tel |
Phone number box (number pad on phones) | <input type="tel" name="phone"> |
url |
Text box that checks for a web address | <input type="url" name="portfolio"> |
search |
Search box, often with a clear (x) button | <input type="search" name="q"> |
date |
Date picker | <input type="date" name="dob"> |
time |
Time picker | <input type="time" name="slot"> |
checkbox |
Tick box, on or off | <input type="checkbox" name="agree"> |
radio |
Pick one from a group | <input type="radio" name="gender" value="f"> |
file |
Button to choose a file | <input type="file" name="resume" accept=".pdf"> |
range |
A slider | <input type="range" name="level" min="1" max="10"> |
color |
Colour picker | <input type="color" name="theme"> |
hidden |
Not shown; sends a fixed value | <input type="hidden" name="source" value="website"> |
submit |
A button that sends the form | <input type="submit" value="Register"> |
reset |
A button that clears the form | <input type="reset" value="Clear"> |
A few notes:
- password only hides the text on screen. The data is protected in transit by HTTPS, not by this input type.
- file needs
method="post"andenctype="multipart/form-data"on the form, or the file will not be sent. - If a browser does not support a type, it shows a plain text box instead, so the form still works.
Built-in form validation
HTML can check values before the form is sent:
| Attribute | What it checks | Example |
|---|---|---|
required |
The field is not empty | <input name="name" required> |
minlength / maxlength |
Length of text | <input name="pwd" type="password" minlength="8"> |
min / max |
Smallest and largest number or date | <input type="number" name="marks" min="0" max="100"> |
pattern |
Matches a regular expression | <input name="pin" pattern="[0-9]{6}"> |
type="email" / type="url" |
Correct shape | <input type="email" name="email"> |
Example: an Indian PIN code field that accepts exactly six digits.
<label for="pin">PIN code</label>
<input type="text" id="pin" name="pin" pattern="[0-9]{6}" required
title="Enter a 6-digit PIN code">
If the user types 5000 and submits, the browser blocks the form and shows a message that includes the title text.
Common mistake: Relying only on HTML validation. Anyone can turn it off with browser developer tools, or send data to your server without using the form at all. Always validate again on the server.
Buttons in forms
<button type="submit">Register</button>
<button type="reset">Clear all</button>
<button type="button">Show preview</button>
| Type | What it does |
|---|---|
submit |
Sends the form (this is the default inside a form) |
reset |
Clears every field back to its starting value |
button |
Does nothing on its own; used with JavaScript |
Because submit is the default, a plain <button> inside a form will submit it. When you add a button for JavaScript, such as "Show preview", always write type="button".
Practice tasks
- Build a student registration form with name, email, password (at least 8 characters), date of birth, gender (radio) and branch (select).
- Add a file input for a resume that accepts only PDF files.
- Build a feedback form with a 1 to 10 range slider and a textarea.
- Build a login form with
method="post"and a "Remember me" checkbox.
Quick revision
<form>wraps fields;actionsays where to send,methodsays how.- Every field needs a
name, or it will not be sent. - Link each
<label for>to an inputid. - The
typeattribute decides the kind of input. - Use
required,min,maxandpatternfor quick checks, and validate on the server too.
Next, learn to show data in rows and columns with HTML tables and lists, or check yourself with our HTML interview questions.
Frequently Asked Questions
What is the form tag in HTML?
How many input types are there in HTML?
Why is the name attribute important in a form?
What is the difference between GET and POST in a form?
Does HTML validation replace server validation?
How do I group radio buttons?
Want to Build Your Career in Java Full Stack with AI?
Join CodeBegun and train with working industry engineers — Discover CodeBegun's Java Full Stack track

