DEV Community

Cover image for 10 Essential HTML Input Types with Examples
Shrihari
Shrihari

Posted on

10 Essential HTML Input Types with Examples

HTML input types are an essential part of form building in web development. They allow users to enter data, make selections, and submit information easily. In this article, we will explore 10 essential HTML input types with examples, showcasing their functionality and best practices. Letโ€™s dive in!

  • Text Input
  • Password Input
  • Email Input
  • Number Input
  • Date Input
  • Checkbox Input
  • Radio Input
  • File Input
  • Range Input
  • Submit Input

Text Input
The element is the most commonly used input type. It creates a single-line text input field where users can enter alphanumeric characters. Here's an example:

<label for="name">Name:</label>
<input type="text" id="name" name="name">
Enter fullscreen mode Exit fullscreen mode

In the above code snippet, we have created a text input field for the userโ€™s name. The for attribute in the tag should match the id attribute of the associated input element. This helps with accessibility and improves the user experience.

Password Input
To create a password input field that obscures the text entered, we can use the element. Here's an example:

<label for="password">Password:</label>
<input type="password" id="password" name="password">
Enter fullscreen mode Exit fullscreen mode

The above code snippet demonstrates a password input field where the entered characters are masked (usually with asterisks) for security purposes. It is important to note that passwords should never be stored in plain text on the server-side.

Email Input
For capturing email addresses, we can use the element. This input type validates that the entered value is in a valid email format. Here's an example:

<label for="email">Email:</label>
<input type="email" id="email" name="email">
Enter fullscreen mode Exit fullscreen mode

By using the type="email" attribute, the browser will validate that the entered value is a properly formatted email address.

Number Input
The element allows users to enter numeric values. It provides validation and allows for additional attributes such as minimum and maximum values. Here's an example:

<label for="quantity">Quantity:</label>
<input type="number" id="quantity" name="quantity" min="1" max="10">

Enter fullscreen mode Exit fullscreen mode

In this example, we have defined an input field where users can enter a quantity between 1 and 10 (inclusive). The min and max attributes control the minimum and maximum allowed values, respectively.

Date Input
To enable users to select a date, we can use the element. It opens a calendar widget for ease of selection. Here's an example:

<label for="birthdate">Birthdate:</label>
<input type="date" id="birthdate" name="birthdate">
Enter fullscreen mode Exit fullscreen mode

The above code snippet creates a date input field. When the user clicks on the field, a date picker/calendar will pop up, allowing them to choose a date conveniently.

Checkbox Input
The element allows users to select one or multiple options from a predefined set of choices. Here's an example:

<label for="food">Select your favorite food:</label><br>
<input type="checkbox" id="pizza" name="food" value="pizza">
<label for="pizza">Pizza</label><br>
<input type="checkbox" id="burger" name="food" value="burger">
<label for="burger">Burger</label><br>
<input type="checkbox" id="sushi" name="food" value="sushi">
<label for="sushi">Sushi</label><br>
Enter fullscreen mode Exit fullscreen mode

In this example, we have created a set of checkbox elements where users can select their favorite food items. The value attribute is used to capture the selected value(s) on the server-side.

Radio Input
The element is used when users need to select a single option from a predefined set of choices. Here's an example:

<label for="gender">Select your gender:</label><br>
<input type="radio" id="male" name="gender" value="male">
<label for="male">Male</label><br>
<input type="radio" id="female" name="gender" value="female">
<label for="female">Female</label><br>
<input type="radio" id="other" name="gender" value="other">
<label for="other">Other</label><br>

Enter fullscreen mode Exit fullscreen mode

In the above code snippet, we have created a set of radio button elements for selecting the userโ€™s gender. The name attribute groups the radio buttons together, while the value attribute captures the selected value.

File Input
To allow users to upload files, we can use the element. Here's an example:

<label for="avatar">Choose an avatar:</label>
<input type="file" id="avatar" name="avatar">
Enter fullscreen mode Exit fullscreen mode

In this example, the user can browse their computer to select a file to upload. The server-side code will handle processing and storing the uploaded file.

Range Input
The element creates a slider control where users can select a value from a range of values. Here's an example:

<label for="volume">Volume:</label>
<input type="range" id="volume" name="volume" min="0" max="100">
Enter fullscreen mode Exit fullscreen mode

In the above code snippet, we have created a range input field that lets users select a volume level between 0 and 100. The min and max attributes define the range.

Submit Input
The element is used to submit form data to the server. Here's an example:

<input type="submit" value="Submit">
Enter fullscreen mode Exit fullscreen mode

In this example, the user can click the submit button to send the form data to the server for processing.

Conclusion
HTML provides a variety of input types to capture different types of data and user interactions. By utilizing these input types effectively, you can create user-friendly forms that enhance the overall user experience. Experiment with these input types and explore their unique attributes to build dynamic and interactive web forms.

Enjoyed reading this ? Please share it with others.

Thanks for the read. Cheers!!!. You are Awesome !

Sharing is Caring. So Share as much as possible ;-)

Top comments (0)