#24 HTML Input types

In HTML <input type=" "> is an important element of HTML form.

The “type” attribute of the input element can be various types, which defines the information field. Such as <input type="text" name="name"> gives a text box.

HTML Input Types

Here are the different input types you can use in HTML

  • <input type="button">
  • <input type="checkbox">
  • <input type="color">
  • <input type="date">
  • <input type="datetime-local">
  • <input type="email">
  • <input type="file">
  • <input type="hidden">
  • <input type="image">
  • <input type="month">
  • <input type="number">
  • <input type="password">
  • <input type="radio">
  • <input type="range">
  • <input type="reset">
  • <input type="search">
  • <input type="submit">
  • <input type="tel">
  • <input type="text">
  • <input type="time">
  • <input type="url">
  • <input type="week">

We will see the example of all these type at last.

Input Type Text

We already seen in previous examples. <input type="text"> defines a single-line text input field

<form>
  <label for="fname">First name:</label><br>
  <input type="text" id="fname" name="fname"><br>
  <label for="lname">Last name:</label><br>
  <input type="text" id="lname" name="lname">
</form>

Input Type Password

<input type="password"> defines a password field. The entered text in the password filed converted into “*” asterisks or circles so that it cannot be read by another user.

<form>
  <label for="username">Username:</label><br>
  <input type="text" id="username" name="username"><br>
  <label for="pwd">Password:</label><br>
  <input type="password" id="pwd" name="pwd">
</form>

Input Type Submit

<input type="submit"> defines a button for submitting form data to a form-handler.

The form-handler is typically a server page with a script for processing input data.

The form-handler is specified in the form’s action attribute

<form action="action.php">  
    <label>Enter User name</label><br>  
    <input type="text" name="firstname"><br>  
    <label>Enter Password</label><br>  
    <input type="Password" name="password"><br>  
    <br><input type="submit" value="submit">  
</form>  

If you omit the submit button’s value attribute, the button will get a default text (task for you. try it)

Input Type Reset

<input type="reset"> used to make reset button that will reset all form values to their default values:

<form action="action.php">  
    <label>Enter User name</label><br>  
    <input type="text" name="firstname"><br>  
    <label>Enter Password</label><br>  
    <input type="Password" name="password"><br>  
    <br><input type="submit" value="submit">  
    <br><input type="reset">  
</form>  

Input Type Radio

<input type="radio"> defines a radio button.

which allow choosing an option between a set of related options. At a time only one radio button option can be selected at a time.

<form>  
  <p>Kindly Select your favorite color</p>  
  <input type="radio" name="color" value="red"> Red <br>  
  <input type="radio" name="color" value="blue"> blue <br>  
  <input type="radio" name="color" value="green">green <br>  
  <input type="radio" name="color" value="pink">pink <br>   
</form>

Input Type Checkbox

<input type="checkbox"> defines a checkbox.

It displayed as square boxes which can be checked or unchecked to select the choices from the given options.

<form>
  <input type="checkbox" id="vehicle1" name="vehicle1" value="Bike">
  <label for="vehicle1"> I have a bike</label><br>
  <input type="checkbox" id="vehicle2" name="vehicle2" value="Car">
  <label for="vehicle2"> I have a car</label><br>
  <input type="checkbox" id="vehicle3" name="vehicle3" value="Boat">
  <label for="vehicle3"> I have a boat</label>
</form>

Input Type Button

<input type="button"> defines a button.

It defines a simple push button, which can be programmed to control a functionally on any event such as, click event.

<form>  
     <input type="button" value="Click me " onclick="alert('you are learning HTML')">  
</form>  

Input Type Color

The <input type="color"> is used for input fields that should contain a color.

 It allows a user to specify the colour by the visual color interface on a browser.

Depending on browser support, a color picker can show up in the input field.

<form>
  <label for="favcolor">Select your favorite color:</label>
  <input type="color" id="favcolor" name="favcolor">
</form>

Input Type Date

The <input type="date"> is used for input fields that should contain a date.

This allows a user to input the date in a given format. A user can enter the date by text field or by date picker interface.

Depending on browser support, a date picker can show up in the input field.

<form>
  <label for="birthday">Birthday:</label>
  <input type="date" id="birthday" name="birthday">
</form>

You can also use the min and max attributes to add restrictions to dates:

<form>
  <label for="datemax">Enter a date before 1980-01-01:</label>
  <input type="date" id="datemax" name="datemax" max="1979-12-31"><br><br>
  <label for="datemin">Enter a date after 2000-01-01:</label>
  <input type="date" id="datemin" name="datemin" min="2000-01-02">
</form>

Input Type Datetime-local

The <input type="datetime-local"> specifies a date and time input field, with no time zone.

It creates input filed which allow a user to select the date as well as local time in the hour and minute without time zone information.

Depending on browser support, a date picker can show up in the input field.

<form>
  <label for="birthdaytime">Birthday (date and time):</label>
  <input type="datetime-local" id="birthdaytime" name="birthdaytime">
</form>

Input Type Email

The <input type="email"> is used for input fields that should contain an e-mail address.

Depending on browser support, the e-mail address can be automatically validated when submitted.

Some smartphones recognize the email type, and add “.com” to the keyboard to match email input.

<form>
  <label for="email">Enter your email:</label>
  <input type="email" id="email" name="email">
</form>

Input Type File

The <input type="file"> defines a file-select field and a “Browse” button for file uploads.

Once you select the file, and after submission, this file can be uploaded to the server with the help of JS code and file API.

<form>
  <label for="myfile">Select a file:</label>
  <input type="file" id="myfile" name="myfile">
</form>

Input Type Month

The <input type="month"> allows the user to select a month and year.

It allows a user to easily enter month and year in the format of “MM, YYYY” where MM defines month value, and YYYY defines the year value.

Depending on browser support, a date picker can show up in the input field.

<form>
  <label for="bdaymonth">Birthday (month and year):</label>
  <input type="month" id="bdaymonth" name="bdaymonth">
</form>

Input Type Number

The <input type="number"> defines a numeric input field.

It allows a user to enter the numeric value. You can also restrict to enter a minimum and maximum value using min and max attribute.

The following example displays a numeric input field, where you can enter a value from 1 to 5:

<form>
  <label for="quantity">Quantity (between 1 and 5):</label>
  <input type="number" id="quantity" name="quantity" min="1" max="5">
</form>

Input Restrictions

Here is a list of some common input restrictions

AttributeDescription
checkedSpecifies that an input field should be pre-selected when the page loads (for type=”checkbox” or type=”radio”)
disabledSpecifies that an input field should be disabled
maxSpecifies the maximum value for an input field
maxlengthSpecifies the maximum number of character for an input field
minSpecifies the minimum value for an input field
patternSpecifies a regular expression to check the input value against
readonlySpecifies that an input field is read only (cannot be changed)
requiredSpecifies that an input field is required (must be filled out)
sizeSpecifies the width (in characters) of an input field
stepSpecifies the legal number intervals for an input field
valueSpecifies the default value for an input field

You will learn more about input restrictions in the next chapter.

The following example displays a numeric input field, where you can enter a value from 0 to 100, in steps of 10. The default value is 30

<form>
  <label for="quantity">Quantity:</label>
  <input type="number" id="quantity" name="quantity" min="0" max="100" step="10" value="30">
</form>

Input Type Range

The <input type="range"> defines a control for entering a number whose exact value is not important (like a slider control).

The default range is 0 to 100. However, you can set restrictions on what numbers are accepted with the minmax, and step attributes. (try step attribute)

<form>
  <label for="vol">Volume (between 0 and 50):</label>
  <input type="range" id="vol" name="vol" min="0" max="50">
</form>

The <input type="search"> is used for search fields (a search field behaves like a regular text field).

<form>
  <label for="gsearch">Search Google:</label>
  <input type="search" id="gsearch" name="gsearch">
</form>

Input Type Tel

The <input type="tel"> is used for input fields that should contain a mobile number.

The “tel” type does not have default validation such as email, because telephone number pattern can vary worldwide.

<form>
  <label for="phone">Enter your phone number:</label>
  <input type="tel" id="phone" name="phone" pattern="[0-9]{3}-[0-9]{2}-[0-9]{3}">
</form>

Input Type Time

The <input type="time"> allows the user to select a time (no time zone).

Depending on browser support, a time picker can show up in the input field.

<form>
  <label for="appt">Select a time:</label>
  <input type="time" id="appt" name="appt">
</form>

Input Type Url

The <input type="url"> is used for input fields that should contain a URL address.

Depending on browser support, the url field can be automatically validated when submitted.

Some smartphones recognize the url type, and adds “.com” to the keyboard to match url input.

<form>
  <label for="homepage">Add your homepage:</label>
  <input type="url" id="homepage" name="homepage">
</form>

Input Type Week

The <input type="week"> allows the user to select a week and year.

It allows a user to select a week and year form the drop-down calendar without time zone.

Depending on browser support, a date picker can show up in the input field.

<form>
  <label for="week">Select a week:</label>
  <input type="week" id="week" name="week">
</form>

Example of all above types

See the Pen input type by Arpit (@soniarpit) on CodePen.

Hope you enjoyed. Try this all types. Happy coding 🙂

Previous: #23 HTML Form Elements

Next: #25 HTML Input Attributes

Support CodeSnail

If you appreciate my work, or if it has helped you along your journey. It would mean a lot to me if you could write a message on my wall and share a cup of coffee (or tea) with me.

Buy Me A Coffee

Leave a Comment

Copy link
Powered by Social Snap