HTML input types determine what kind of information users can enter or select in a form. The <input> element supports different types, including text, password, email, number, date, checkbox, radio, and file.
Choosing the correct input type helps browsers display appropriate controls, provide basic validation, and improve the user experience on different devices.
What Are HTML Input Types?
HTML input types are values assigned to the type attribute of an <input> element. Each type defines how the field behaves.
For example:
<label for="email">Email:</label>
<input type="email" id="email" name="email">
The email type creates a field intended for email addresses. Browsers can perform basic email-format validation when the form is submitted.
To understand how input fields fit into a form, explore HTML Input.
Common HTML Input Types
HTML provides several input types for different purposes.
| Input Type | Purpose |
|---|---|
text | Accepts general text |
password | Masks entered characters |
email | Accepts an email address |
number | Accepts numeric values |
date | Allows users to select a date |
time | Allows users to select a time |
radio | Selects one option from a group |
checkbox | Selects one or more options |
file | Allows users to select a file |
range | Displays a slider |
color | Opens a color picker |
search | Provides a search field |
submit | Submits a form |
reset | Resets form controls to their initial values |
button | Creates a general-purpose button |
HTML Input Types Example
The following example demonstrates several commonly used input types.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML Input Types</title>
</head>
<body>
<h1>HTML Input Types</h1>
<form action="/register" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password">
<br><br>
<label for="birthdate">Date of Birth:</label>
<input type="date" id="birthdate" name="birthdate">
<br><br>
<label for="age">Age:</label>
<input type="number" id="age" name="age" min="1" max="120">
<br><br>
<button type="submit">Register</button>
</form>
</body>
</html>
In this example, users can enter a name, email address, and password, select a birth date, and enter an age. The required attribute makes the name and email fields mandatory during normal browser validation.
The /register endpoint must exist on a server to process submitted data.
How to Choose the Right Input Type
Choose an input type based on the information you need to collect. Use email for email addresses, date for dates, and number for numeric quantities. Use checkboxes when users can select multiple options and radio buttons when they must choose one option from a group.
For more specific examples, explore HTML Text Fields and HTML Password Fields.
Benefits of HTML Input Types
HTML input types improve form usability by displaying appropriate controls, supporting mobile-friendly input methods, and providing basic browser validation. They can also reduce the amount of custom code needed to build common form fields.
However, browser validation is not a substitute for server-side validation. Applications should verify submitted data before processing or storing it.
Frequently Asked Questions
How many HTML input types are available?
HTML supports numerous input types, including text, email, password, number, date, radio, checkbox, file, range, color, and search.
What is the default input type?
If the type attribute is omitted or has an invalid value, the input generally behaves as a text field.
Can I use multiple input types in one form?
Yes. A single form can contain different input types depending on the information being collected.
Which input type is best for email addresses?
Use type="email" because it provides an appropriate field and basic browser validation for email formatting.