HTML Input Types






<!--
main_leaderboard, all: [728,90][970,90][320,50][468,60]
-->



HTML Input Types



❮ Previous
Next ❯



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




Input Type Text


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



Example



<form>
 
First name:<br>
  <input type="text" name="firstname"><br>
 
Last name:<br>
  <input type="text" name="lastname">
</form>

Try it Yourself »

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



<form>
 
User name:<br>
  <input type="text" name="username"><br>
 
User password:<br>
  <input type="password" name="psw">
</form>

Try it Yourself »

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


User name:

User password:

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








<!--
mid_content, all: [300,250][336,280][728,90][970,250][970,90][320,50][468,60]
-->





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



<form action="/action_page.php">
  First name:<br>
  <input type="text"
name="firstname" value="Mickey"><br>
  Last name:<br>
  <input
type="text" name="lastname" value="Mouse"><br><br>
  <input type="submit"
value="Submit">
</form>

Try it Yourself »

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



<form action="/action_page.php">
  First name:<br>
  <input type="text"
name="firstname" value="Mickey"><br>
  Last name:<br>
  <input
type="text" name="lastname" value="Mouse"><br><br>
  <input type="submit">
</form>

Try it Yourself »



Input Type Reset


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



Example



<form action="/action_page.php">
  First name:<br>
  <input type="text"
name="firstname" value="Mickey"><br>
  Last name:<br>
  <input
type="text" name="lastname" value="Mouse"><br><br>
  <input type="submit"
value="Submit">
  <input type="reset">
</form>

Try it Yourself »

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



<form>
  <input type="radio" name="gender" value="male"
checked> Male<br>
  <input
type="radio" name="gender" value="female"> Female<br>
  <input
type="radio" name="gender" value="other"> Other
</form>

Try it Yourself »

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


Male
Female
Other


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



<form>
  <input type="checkbox" name="vehicle1" value="Bike"> I have a bike<br>
 
<input type="checkbox" name="vehicle2" value="Car"> I have a car
</form>

Try it Yourself »

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


I have a bike

I have a car


Input Type Button


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



Example



<input type="button" onclick="alert('Hello World!')" value="Click Me!">

Try it Yourself »

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




HTML5 Input Types


HTML5 added several new input types:



  • color

  • date

  • datetime-local

  • email

  • month

  • number

  • range

  • search

  • tel

  • time

  • url

  • week



New input types that are not supported by older web browsers, will behave as <input type="text">.





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.



OperaSafariChromeFirefoxInternet Explorer

Example



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

Try it Yourself »



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.



OperaSafariChromeFirefoxInternet Explorer

Example



<form>
 
Birthday:
  <input type="date" name="bday">
</form>

Try it Yourself »


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



OperaSafariChromeFirefoxInternet Explorer

Example



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

Try it Yourself »



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.



OperaSafariChromeFirefoxInternet Explorer

Example



<form>
 
Birthday (date and time):
  <input type="datetime-local" name="bdaytime">
</form>

Try it Yourself »



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 adds ".com" to the keyboard to match email input.



OperaSafariChromeFirefoxInternet Explorer

Example



<form>
 
E-mail:
  <input type="email" name="email">
</form>

Try it Yourself »



Input Type File


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



OperaSafariChromeFirefoxInternet Explorer

Example



<form>
  Select a file: <input type="file" name="myFile">
</form>

Try it Yourself »



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.



OperaSafariChromeFirefoxInternet Explorer

Example



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

Try it Yourself »



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:



OperaSafariChromeFirefoxInternet Explorer

Example



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

Try it Yourself »



Input Restrictions


Here is a list of some common input restrictions (some are new in HTML5):















































AttributeDescription
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:




OperaSafariChromeFirefoxInternet Explorer

Example



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

Try it Yourself »



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:



OperaSafariChromeFirefoxInternet Explorer

Example



<form>
 
<input type="range" name="points" min="0" max="10">
</form>

Try it Yourself »



Input Type Search


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



OperaSafariChromeFirefoxInternet Explorer

Example



<form>
 
Search Google:
  <input type="search" name="googlesearch">
</form>

Try it Yourself »



Input Type Tel


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


Note: The tel type is currently only supported in Safari 8.



OperaSafariChromeFirefoxInternet Explorer

Example



<form>
 
Telephone:
  <input type="tel" name="usrtel">
</form>

Try it Yourself »



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.



OperaSafariChromeFirefoxInternet Explorer

Example



<form>
 
Select a time:
  <input type="time" name="usr_time">
</form>

Try it Yourself »



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.



OperaSafariChromeFirefoxInternet Explorer

Example



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

Try it Yourself »




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.



OperaSafariChromeFirefoxInternet Explorer

Example



<form>
 
Select a week:
  <input type="week" name="week_year">
</form>

Try it Yourself »




Test Yourself with Exercises!



Exercise 1 »   
Exercise 2 »   
Exercise 3 »   
Exercise 4 »   
Exercise 5 »




HTML Input Type Attribute











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



❮ Previous
Next ❯

Popular posts from this blog

Colors HSL

W3.CSS Downloads

W3.CSS Tables