Table of contents
1.
Introduction
2.
Input Type "text"
2.1.
Example Output
3.
Input Type "password"
3.1.
Example Output
4.
Input Type "submit"
4.1.
Example Output
5.
Input Type "color"
5.1.
Example Output
6.
Input Type "date"
6.1.
Example Output
7.
Input Type "email"
7.1.
Example Output
8.
Input Type "file"
8.1.
Example Output
9.
Input Type "hidden"
9.1.
Example Output
10.
Input Type "image"
10.1.
Example Output
11.
Input Type "url"
11.1.
Example Output
12.
Input Type "time"
12.1.
Example Output
13.
Input Type "number"
13.1.
Example Output
14.
Input Type "radio"
14.1.
Example Output
15.
Input Type "range"
15.1.
Example Output
16.
Input Type "reset"
16.1.
Example Output
17.
Input Type "search"
17.1.
Example Output
18.
Input Type "button"
18.1.
Example Output
19.
Input Type "tel"
19.1.
Example Output
20.
Input Type "week"
20.1.
Example Output
21.
Input Type "month"
21.1.
Example Output
22.
Input Type "datetime-local"
22.1.
Example Output
23.
Input Type "checkbox"
23.1.
Example Output
24.
Input Type Month  
25.
Input Type Number  
26.
Input Restrictions  
26.1.
Example Code for Input Restrictions  
26.2.
Why Use Input Restrictions?  
27.
Frequently Asked Questions
27.1.
What is the purpose of different input types in HTML?
27.2.
How do I choose the right input type?
27.3.
Can input types affect form validation?
28.
Conclusion
Last Updated: Feb 23, 2025
Medium

HTML Form Input Types

Author Gaurav Gandhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

HTML provides various form input types to collect different kinds of user data efficiently. These input types help enhance user experience by offering appropriate fields such as text, password, email, number, checkbox, radio, date, and more. Each type serves a specific purpose, ensuring accurate data collection and validation. 

HTML Form Input Types

In this article, you will learn about different HTML form input types, their usage, and practical examples.

Input Type "text"

This type is used for single-line text input.

<input type="text" name="username" placeholder="Enter your name">

Example Output

A text box where users can type their name.

Input Type "password"

Used for entering passwords. It hides characters for security.

<input type="password" name="password" placeholder="Enter your password">

Example Output

A password field with hidden characters.

Input Type "submit"

This button submits form data to a server.

<input type="submit" value="Submit">

Example Output

A clickable submit button.

Input Type "color"

Allows users to pick a color.

<input type="color" name="favcolor">

Example Output

A color picker.

Input Type "date"

Allows users to select a date.

<input type="date" name="birthdate">

Example Output

A date picker.

Input Type "email"

Validates email input.

<input type="email" name="useremail" placeholder="Enter your email">

Example Output

An email input field that ensures proper email format.

Input Type "file"

Allows users to upload files.

<input type="file" name="uploadfile">

Example Output

A file upload button.

Input Type "hidden"

Stores hidden values.

<input type="hidden" name="userID" value="12345">

Example Output

Not visible to users.

Input Type "image"

Uses an image as a submit button.

<input type="image" src="submit.png" alt="Submit" width="50">

Example Output

An image button that submits the form.

Input Type "url"

Accepts website URLs.

<input type="url" name="website" placeholder="Enter website URL">

Example Output

A URL input field.

Input Type "time"

Allows users to select time.

<input type="time" name="appointment">

Example Output

A time picker.

Input Type "number"

Allows only numeric values.

<input type="number" name="quantity" min="1" max="10">

Example Output

A number input field with restrictions.

Input Type "radio"

Used for selecting one option from multiple choices.

<input type="radio" name="gender" value="male"> Male
<input type="radio" name="gender" value="female"> Female

Example Output

A set of radio buttons.

Input Type "range"

Provides a slider for selecting a value.

<input type="range" name="volume" min="0" max="100">

Example Output

A slider to adjust values.

Input Type "reset"

Resets form fields.

<input type="reset" value="Reset">

Example Output

A button to reset form fields.

Input Type "search"

Optimized for search input.

<input type="search" name="query" placeholder="Search here">

Example Output

A search box.

Input Type "button"

Creates a simple button.

<input type="button" value="Click Me">

Example Output

A clickable button.

Input Type "tel"

For entering telephone numbers.

<input type="tel" name="phone" placeholder="Enter phone number">

Example Output

A telephone input field.

Input Type "week"

Allows selecting a week.

<input type="week" name="week">

Example Output

A week selector.

Input Type "month"

Allows selecting a month.

<input type="month" name="month">

Example Output

A month selector.

Input Type "datetime-local"

Allows selecting date and time together.

<input type="datetime-local" name="event">

Example Output

A date-time picker.

Input Type "checkbox"

Used for multiple-choice options.

<input type="checkbox" name="agree" value="yes"> I agree to terms

Example Output

A checkbox for user agreement.

Input Type Month  

The `month` input type is used to let users select a specific month and year. It’s helpful when you need to collect data like birth months, project deadlines, or any event scheduling. This input type provides a user-friendly interface where users can pick a month from a dropdown calendar instead of typing it manually.  

To use the `month` input type, you need to set the `type` attribute of an `<input>` element to `month`. For example: 

<!DOCTYPE html>  
<html>  
<head>  
    <title>Month Input Example</title>  
</head>  
<body>  
    <h2>Select Your Birth Month</h2>  
    <form>  
        <label for="birthMonth">Choose Month:</label>  
        <input type="month" id="birthMonth" name="birthMonth">  
        <br><br>  
        <input type="submit" value="Submit">  
    </form>  
</body>  
</html>  

 

Output

Output

In this Code:   

1. The `<input>` element has its `type` set to `month`. This tells the browser to display a month picker.  
 

2. The `id` and `name` attributes help identify the input field when the form is submitted.  
 

3. A `<label>` is added to make the form accessible. It links to the input using the `for` attribute.  
 

4. The `submit` button allows users to send their selected month to the server.  
 

When you open this code in a browser, you’ll see a small calendar icon next to the input field. Clicking on it opens a dropdown where users can choose a month and year.  

This input type saves time as users don’t have to type the month manually. It also reduces errors since the format is standardized. However, keep in mind that not all browsers support this input type fully. For unsupported browsers, it falls back to a simple text input.  

Input Type Number  

The `number` input type is designed to let users enter numeric values. It’s commonly used for fields like age, quantity, or ratings. This input type ensures that only numbers can be entered, making it easier to handle data validation. Additionally, it provides up and down arrows (spinners) to increase or decrease the value, which improves user experience.  

For example: 

<!DOCTYPE html>  
<html>  
<head>  
    <title>Number Input Example</title>  
</head>  
<body>  
    <h2>Enter Your Age</h2>  
    <form>  
        <label for="age">Age:</label>  
        <input type="number" id="age" name="age" min="1" max="120" step="1">  
        <br><br>  
        <input type="submit" value="Submit">  
    </form>  
</body>  
</html>  

 

Output

Output

In this Code:   

1. The `<input>` element has its `type` set to `number`. This restricts the input to numeric values only.  
 

2. The `min` attribute specifies the smallest value a user can enter. In this case, it’s set to `1`.  
 

3. The `max` attribute sets the highest allowed value, which is `120` here.  
 

4. The `step` attribute defines the interval between valid numbers. A `step` of `1` means only whole numbers are accepted.  
 

5. The `id` and `name` attributes help identify the field when the form is submitted.  
 

6. The `submit` button allows users to send their entered number to the server.  

 

When you open this code in a browser, you’ll see an input field with up and down arrows. Users can either type a number or use the arrows to adjust the value. If they try entering a non-numeric value or a number outside the specified range, the browser will show an error message.  

This input type is useful because it prevents invalid data from being submitted. For instance, if you’re collecting ages, you don’t want users entering negative numbers or text. The `number` input type handles these issues automatically.  

Input Restrictions  

Input restrictions are rules you can apply to form fields to control what users can enter. These restrictions ensure that the data collected is valid and meets specific criteria. For example, you might want to limit the number of characters in a text field or ensure an email address follows the correct format. Using input restrictions helps reduce errors and improves the overall quality of the data.  

There are several attributes you can use to add restrictions to form inputs. Let’s discuss some common ones with examples:  

Example Code for Input Restrictions  

<!DOCTYPE html>  
<html>  
<head>  
    <title>Input Restrictions Example</title>  
</head>  
<body>  
    <h2>Registration Form</h2>  
    <form>  
        <!-- Text Field with Character Limit -->  
        <label for="username">Username (Max 10 Characters):</label>  
        <input type="text" id="username" name="username" maxlength="10" required>  
        <br><br>  

        <!-- Email Field with Format Validation -->  
        <label for="email">Email Address:</label>  
        <input type="email" id="email" name="email" required>  
        <br><br>  

        <!-- Password Field with Minimum Length -->  
        <label for="password">Password (Min 6 Characters):</label>  
        <input type="password" id="password" name="password" minlength="6" required>  
        <br><br>  

        <!-- Submit Button -->  
        <input type="submit" value="Register">  
    </form>  
</body>  
</html>  

 

Output

Output

In this Code:   

1. Text Field with `maxlength`:  

The `maxlength` attribute limits the number of characters a user can enter. In this example, the username field only allows up to 10 characters. If a user tries to type more, the browser will stop accepting additional input.  

 

2. Email Field with Format Validation:  

The `type="email"` ensures that the input follows a valid email format (e.g., `example@domain.com`). If the user enters an invalid email, the browser will show an error when they try to submit the form.  

 

3. Password Field with `minlength`:  

The `minlength` attribute sets the minimum number of characters required. Here, the password must be at least 6 characters long. If the user enters fewer characters, the form won’t submit.  

 

4. Required Attribute:  

The `required` attribute makes sure the field isn’t left empty. If a user tries to submit the form without filling in a required field, the browser will display an error message.  

Why Use Input Restrictions?  

Input restrictions save time for both users and developers. They prevent invalid data from being submitted, which reduces the need for additional validation on the server side. For example, if you’re collecting email addresses, using the `type="email"` attribute ensures that only properly formatted emails are accepted. This improves the accuracy of the data and enhances the user experience.  

Frequently Asked Questions

What is the purpose of different input types in HTML?

Each input type is designed to handle specific types of data, making forms more user-friendly and functional.

How do I choose the right input type?

Choose an input type based on the data you want users to enter. For example, use email for email addresses and number for numeric values.

Can input types affect form validation?

Yes, some input types, like email and number, enforce validation, ensuring correct data format before submission.

Conclusion

In this article, we discussed HTML form input types, which define the type of data users can enter in a form. Common input types include text, password, email, number, date, checkbox, radio, and file. Choosing the right input type improves user experience, ensures data validation, and enhances form accessibility in web development.

Live masterclass