HTML FormsLesson #45

HTML Input Types

This chapter describes the different types for the HTML `<input>` element.

This chapter describes the different types for the HTML <input> element.


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">

Tip: The default value of the type attribute is "text".


Input Type Text

<input type="text"> defines a single-line text input field:

Example

Example Code

html
Try it Yourself
Live Output Preview:

This is how the HTML code above will be displayed in a browser:

First name:

Last name:


Input Type Password

<input type="password"> defines a password field:

Example

Example Code

html
Try it Yourself
Live Output Preview:

This is how the HTML code above will be displayed in a browser:

Username:

Password:

The characters in a password field are masked (shown as asterisks or circles).


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:

Example

Example Code

html
Try it Yourself
Live Output Preview:

This is how the HTML code above will be displayed in a browser:

First name:

Last name:

If you omit the submit button's value attribute, the button will get a default text:

Example

Example Code

html
Try it Yourself
Live Output Preview:

Input Type Reset

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

Example

Example Code

html
Try it Yourself
Live Output Preview:

This is how the HTML code above will be displayed in a browser:

First name:

Last name:

If you change the input values and then click the "Reset" button, the form-data will be reset to the default values.


Input Type Radio

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

Radio buttons let a user select ONLY ONE of a limited number of choices:

Example

Example Code

html
Try it Yourself
Live Output Preview:

This is how the HTML code above will be displayed in a browser:

HTML

CSS

JavaScript


Input Type Checkbox

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

Checkboxes let a user select ZERO or MORE options of a limited number of choices.

Example

Example Code

html
Try it Yourself
Live Output Preview:

This is how the HTML code above will be displayed in a browser:

I have a bike

I have a car

I have a boat


Input Type Button

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

Example

Example Code

html
Try it Yourself
Live Output Preview:

This is how the HTML code above will be displayed in a browser:


Input Type Color

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

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

Example

Example Code

html
Try it Yourself
Live Output Preview:

Input Type Date

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

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

Example

Example Code

html
Try it Yourself
Live Output Preview:

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

Example

Example Code

html
Try it Yourself
Live Output Preview:

Input Type Datetime-local

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

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

Example

Example Code

html
Try it Yourself
Live Output Preview:

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.

Example

Example Code

html
Try it Yourself
Live Output Preview:

Input Type Image

The <input type="image"> defines an image as a submit button.

The path to the image is specified in the src attribute.

Example

Example Code

html
Try it Yourself
Live Output Preview:

Input Type File

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

Example

Example Code

html
Try it Yourself
Live Output Preview:

Input Type Hidden

The <input type="hidden"> defines a hidden input field (not visible to a user).

A hidden field lets web developers include data that cannot be seen or modified by users when a form is submitted.

A hidden field often stores what database record that needs to be updated when the form is submitted.

Note: While the value is not displayed to the user in the page's content, it is visible (and can be edited) using any browser's developer tools or "View Source" functionality. Do not use hidden inputs as a form of security!

Example

Example Code

html
Try it Yourself
Live Output Preview:

Input Type Month

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

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

Example

Example Code

html
Try it Yourself
Live Output Preview:

Input Type Number

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

You can also set restrictions on what numbers are accepted.

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

Example

Example Code

html
Try it Yourself
Live Output Preview:

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:

Example

Example Code

html
Try it Yourself
Live Output Preview:

Input Type Range

The <input type="range"> defines a control for entering a number whose exact value is not important (like a slider control). Default range is 0 to 100. However, you can set restrictions on what numbers are accepted with the min, max, and step attributes:

Example

Example Code

html
Try it Yourself
Live Output Preview:

Input Type Search

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

Example

Example Code

html
Try it Yourself
Live Output Preview:

Input Type Tel

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

Example

Example Code

html
Try it Yourself
Live Output Preview:

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.

Example

Example Code

html
Try it Yourself
Live Output Preview:

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.

Example

Example Code

html
Try it Yourself
Live Output Preview:

Input Type Week

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

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

Example

Example Code

html
Try it Yourself
Live Output Preview:

HTML Input Type Attribute

TagDescription
<input type="">Specifies the input type to display

Interactive Code Examples

HTML Example 1

html
Try it Yourself
Interactive runnable HTML example from EA Academy.
Live Output Preview:

HTML Example 2

html
Try it Yourself
Interactive runnable HTML example from EA Academy.
Live Output Preview:

HTML Example 3

html
Try it Yourself
Interactive runnable HTML example from EA Academy.
Live Output Preview:

HTML Example 4

html
Try it Yourself
Interactive runnable HTML example from EA Academy.
Live Output Preview:

HTML Example 5

html
Try it Yourself
Interactive runnable HTML example from EA Academy.
Live Output Preview:

HTML Example 6

html
Try it Yourself
Interactive runnable HTML example from EA Academy.
Live Output Preview: