HTML Input types: Which to use when

After reading this article you'll be having clarity on which input type to use and when.

ยท

3 min read

Webpages need to constantly interact with the users to provide personalised results, so there come HTML Forms into the picture. The tag is used to get the data from the users. HTML 5 provides us with a large number of input types to accept the data from the users.

So, Lessgooo and learn about various input types according to their use cases ๐Ÿš€

Text field

This is the default and the most used, it can be implemented using the input type text, it accepts a single line string, and line breaks are not supported in this type.

Attributes

  • placeholder: This is the value written in the input box initially which guides about the input.
  • minlength: It is the minimum length of the input that can be accepted.
  • maxlength: It is the maximum length of the input that can be accepted.
  • spellcheck: It is an attribute which checks for the spelling of the input's value.
    <input
      type="text"
      maxlength="255"
      minlength="10"
      placeholder="I'm a placeholder"
      spellcheck="true"
   />

Screenshot 2022-09-04 at 1.12.06 PM.png

Numeric field

This can be implemented using the input type number. It only accepts floating point numbers and also provides two control buttons for decreasing and increasing the input value.

Attributes

  • min: It is the minimum number that can be accepted.
  • max: It is the maximum number that can be accepted.
  • step: It is an attribute which checks for the spelling of the input's value.
 <input type="number" min="3" max="200" step="0.1" placeholder="I'm a placeholder"/>

Screenshot 2022-09-04 at 1.10.00 PM.png

Email address field

It can be implemented using the input type email, here user needs to enter the valid email address else it will show an error to the user.

we can also use multiple attribute to ask users for more than 1 email address separated by commas.

  <input type="email" placeholder="I'm a placeholder" />

Screenshot 2022-09-04 at 1.08.44 PM.png

Phone number field

It can be implemented using the input type tel.

  <input type="tel" placeholder="I'm a placeholder" />

Screenshot 2022-09-04 at 1.13.45 PM.png

URL field

It can be implemented using the input type url. The browser will return an error if it is an invalid URL or no protocol (HTTP/HTTPS) is entered.

  <input type="url" placeholder="I'm a placeholder" />

Screenshot 2022-09-04 at 1.15.02 PM.png

Slider controls

It can be implemented using the input type range. Slider is another way to get a number as an input, it has a max value, and min value and the user can only input values between those two values. Like input type number it also provides step attribute.

  <input type="range" min="0" max="100" step="5"/>

Screenshot 2022-09-04 at 1.16.24 PM.png

Color picker control

It can be implemented using the input type color. On clicking color control a dialogue box will appear to select color according to your operating system. It returns a 6-digit lowercase hexadecimal value.

  <input type="color" />

Screenshot 2022-09-04 at 1.20.23 PM.png

Date and time pickers

There are various input types for getting dates and times as input.

date input type is used to get a date as input, it opens a calendar in which we can select dates.

  <input type="date" />

Screenshot 2022-09-04 at 1.22.17 PM.png

time input type is used to get a time as an input, it takes time in 12-hour format but stores the result value in 24-hours format.

  <input type="time" />

Screenshot 2022-09-04 at 1.24.43 PM.png

datetime-local input type is a kind of mixture of both input type date and input type time.

  <input type="datetime-local" />

Screenshot 2022-09-04 at 1.26.37 PM.png

Thank you for reading, I'll appreciate your feedback and doubts in the comments.

Happy Coding ๐Ÿš€

Wishing you all the best for further learning!

Did you find this article valuable?

Support Kunaaal's blog by becoming a sponsor. Any amount is appreciated!

ย