HTML Forms Intro


What is a Form

Forms are a simple way that a person viewing your Web page can send you data. This is done by the user filling in various parts or input lines within the form designated by you.

As you will see, designing forms can be quite an undertaking on the Web. They introduce a kind of interactivity that you can’t get from other kinds of media like television or magazines. Forms, and the capabilities they provide to enable two-way communication between the website creator and the website visitor, are what makes the Web a truly unique publishing platform.

Forms are supported by almost every browser and make it possible to create documents that collect and process user input and formulate personalized replies. Once a user fills out a form, it is submitted to a server or e-mailed to a specific address. If sent to a server, that server passes that information to a supporting program or application that processes the data.

Design Layouts

Designing and styling forms and getting constant results across browsers and platforms can be the most difficult part of web development.

Most of this difficulty arises because of the nature of form elements. They’re built to solicit input from the user of the site, and how that input gets there can be quite different from device to device.

Different operating systems have different native form control behavior and appearance, and browser vendors have tried to keep the display of interface elements in line with those standards. As a result, a user will typically see familiar inputs even if they use multiple browsers on one device, but if they change devices or operating systems, the interface elements will change. Even in the same browser, the appearances may differ between Windows, Mac OS X, Linux, and mobile devices. Below is an example that shows the difference in appearance of a select element on three different devices.


Interaction with select element in (from left) Safari/Apple iPhone iOS8, Safari/Apple iPad iOS8, and IE 9/Windows 7.

Something else that can be unique to form layouts is the need to juggle the myriad of positioning and placement and states of the elements of the form. Large fields, small fields, sets of fields, labels, and help or error messages all need to be placed so that it is clear to the visitor what is being requested from them. The grid that works for a standard-length text input along with its label may not work for a collection of radio buttons or a combination of inputs such as parts of a phone number or city and state.

Functionality

A form consists of two main components. First, one or more input fields into which the visitor types or clicks the information you have requested. Second, a "submit" button which, when clicked, sends the contents of the form to a server-side program for processing in whatever way it wishes.

Filling out forms online is a great way to glean information from your visitors or provide enhanced service to them. Although the scripts that actually receive and process such form data are beyond the scope of these lessons, the next issue is how some of the various form elements work in HTML markup. The first thing you’ll need in any form you create is the form element:

<form>
</form>

Although form elements accept many attributes, only one, the action attribute, is required. The action attribute is similar to the href attribute in the <a> element. It specifies a particular URL where the script that processes the form data can be found.

A form element is a lot like a <blockquote> in the sense that it can have only certain child elements. For simple forms, you can use the <p> or <div> elements that you’ve already seen used before, and you can place all the form’s controls into these elements. However, there are more complex forms that you may encounter that will require a HTML element specifically designed to group form controls into groups: the <fieldset> element.

 







HTML Form Elements


This chapter describes all HTML form elements.


the <input> Element

the most important form element is the <input> element.

the <input> element can vary in many ways, depending on the type attribute.

Note All HTML input types are covered in the next chapter.


the <select> Element (Drop-Down List)

the <select> element defines a drop-down list:

Example

<select name="cars">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="fiat">Fiat</option>
  <option value="audi">Audi</option>
</select>
Try it yourself »

the <option> elements defines the options to select.

the list will normally show the first item as selected.

You can add a selected attribute to define a predefined option.

Example

<option value="fiat" selected>Fiat</option>
Try it yourself »

the <textarea> Element

the <textarea> element defines a multi-line input field (a text area):

Example

<textarea name="message" rows="10" cols="30">
the cat was playing in the garden.
</textarea>
Try it yourself »

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



the <button> Element

the <button> element defines a clickable button:

Example

<button type="button" onclick="alert('Hello World!')">Click Me! </button>
Try it yourself »

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



HTML5 Form Elements

HTML5 added the following form elements:

Note

By default, browsers do not display unknown elements. New elements will not destroy your page.


HTML5 <datalist> Element

the <datalist> element specifies a list of pre-defined options for an <input> element.

Users will see a drop-down list of pre-defined options as they input data.

the list attribute of the <input> element, must refer to theid attribute of the <datalist> element.

Opera Safari Chrome Firefox Internet Explorer

Example

An <input> element with pre-defined values in a <datalist>:

<form action="action_page.php">
 <input list="browsers">
 <datalist id="browsers">
    <option value="Internet Explorer">
   <option value="Firefox">
    <option value="Chrome">
   <option value="Opera">
    <option value="Safari">
 </datalist>
</form>
Try it yourself »

HTML5 <keygen> Element

the purpose of the <keygen> element is to provide a secure way to authenticate users.

the <keygen> element specifies a key-pair generator field in a form.

When the form is submitted, two keys are generated, one private and one public.

the private key is stored locally, and the public key is sent to the server.

the public key could be used to generate a client certificate to authenticate the user in the future.

Opera Safari Chrome Firefox Internet Explorer

Example

A form with a keygen field:

<form action="action_page.php">
  Username: <input type="text" name="user">
  Encryption: <keygen name="security">
 <input type="submit">
</form>
Try it yourself »

HTML5 <output> Element

the <output> element represents the result of a calculation (like one performed by a script).

Opera Safari Chrome Firefox Internet Explorer

Example

Perform a calculation and show the result in an <output> element:

<form action="action_page.asp"
  oninput="x.value=parseInt(a.value)+parseInt(b.value)">
  0
 <input type="range"  id="a" name="a" value="50">
  100 +
 <input type="number" id="b" name="b" value="50">
  =
 <output name="x" for="a b"></output>
  <br><br>
  <input type="submit">
</form>
Try it yourself »

HTML Form Elements

= new in HTML5.

Tag Description
<form> Defines an HTML form for user input
<input> Defines an input control
<textarea> Defines a multiline input control (text area)
<label> Defines a label for an <input> element
<fieldset> Groups related elements in a form
<legend> Defines a caption for a <fieldset> element
<select> Defines a drop-down list
<optgroup> Defines a group of related options in a drop-down list
<option> Defines an option in a drop-down list
<button> Defines a clickable button
<datalist> Specifies a list of pre-defined options for input controls
<keygen> Defines a key-pair generator field (for forms)
<output> Defines the result of a calculation

 







Form Input Types


Input Types

this chapter describes the input types of the <input> element.


Input Type: text

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

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:

Note 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 input 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: 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="sex" value="male" checked> Male
<br>
<input type="radio" name="sex" value="female"> Female
</form>
Try it yourself »

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

Male
Female

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:

Note

Input types, not supported by old web browsers, will behave as input type text.


Input Type: number

the <input type="number"> is used for input fields that should contain a numeric value.

You can set restrictions on the numbers.

Depending on browser support, the restrictions can apply to the input field.

Opera Safari Chrome Firefox Internet 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):

Attribute Description
disabled Specifies that an input field should be disabled
max Specifies the maximum value for an input field
maxlength Specifies the maximum number of character for an input field
min Specifies the minimum value for an input field
pattern Specifies a regular expression to check the input value against
readonly Specifies that an input field is read only (cannot be changed)
required Specifies that an input field is required (must be filled out)
size Specifies the width (in characters) of an input field
step Specifies the legal number intervals for an input field
value Specifies the default value for an input field

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

Opera Safari Chrome Firefox Internet Explorer

Example

<form>
  Quantity:
 <input type="number" name="points" min="0" max="100" step="10" value="30">
</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.

Opera Safari Chrome Firefox Internet Explorer

Example

<form>
  Birthday:
  <input type="date" name="bday">
</form>
Try it yourself »

You can add restrictions to the input:

Opera Safari Chrome Firefox Internet 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: 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.

Opera Safari Chrome Firefox Internet Explorer

Example

<form>
  Select your favorite color:
  <input type="color" name="favcolor">
</form>
Try it yourself »

Input Type: range

the <input type="range">is used for input fields that should contain a value within a range.

Depending on browser support, the input field can be displayed as a slider control.

Opera Safari Chrome Firefox Internet Explorer

Example

<form>
 <input type="range" name="points" min="0" max="10">
</form>
Try it yourself »

You can use the following attributes to specify restrictions: min, max, step, value.


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.

OperaSafari Chrome Firefox Internet Explorer

Example

<form>
  Birthday (month and year):
  <input type="month" name="bdaymonth">
</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.

Opera Safari Chrome Firefox Internet Explorer

Example

lt;form>
  Select a week:
  <input type="week" name="week_year">
</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.

Opera Safari Chrome Firefox Internet Explorer

Example

<form>
  Select a time:
  <input type="time" name="usr_time">
</form>
Try it yourself »

Input Type: datetime

the <input type="datetime"> allows the user to select a date and time (with time zone).

Opera Safari Chrome Firefox Internet Explorer

Example

<form>
  Birthday (date and time):
  <input type="datetime" name="bdaytime">
</form>
Try it yourself »
Note the input type datetime is removed from the HTML standard. Use datetime-local instead.

Input Type: datetime-local

the <input type="datetime-local"> allows the user to select a date and time (no time zone).

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

Opera Safari Chrome Firefox Internet 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.

OperaSafari Chrome Firefox Internet Explorer

Example

<form>
  E-mail:
  <input type="email" name="email">
</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).

Opera Safari Chrome Firefox Internet 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.

the tel type is currently supported only in Safari 8.

Opera Safari Chrome Firefox Internet Explorer

Example

<form>
 Telephone:
  <input type="tel" name="usrtel">
</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.

OperaSafari Chrome Firefox Internet Explorer

Example

<form>
  Add your homepage:
  <input type="url" name="homepage">
</form>
Try it yourself »


 







Form Input Attributes


the value Attribute

the value attribute specifies the initial value for an input field:

Example

<br>
Last name:<br>
<input type="text" name="lastname">
</form>
Try it yourself »

the readonly Attribute

the readonly attribute specifies that the input field is read only (cannot be changed):

Example

<form action="">
First name:<br>
<input type="text" name="firstname" value="John" readonly>
<br>
Last name:<br>
<input type="text" name="lastname">
</form>
Try it yourself »

the readonly attribute does not need a value. It is the same as writing readonly="readonly".


the disabled Attribute

the disabled attribute specifies that the input field is disabled.

A disabled element is un-usable and un-clickable.

Disabled elements will not be submitted.

Example

<form action="">
First name:<br>
<input type="text" name="firstname" value="John" disabled>
<br>
Last name:<br>
<input type="text" name="lastname">
</form>
Try it yourself »

the disabled attribute does not need a value. It is the same as writing disabled="disabled".


the size Attribute

the size attribute specifies the size (in characters) for the input field:

Example

<form action="">
First name:<br>
<input type="text" name="firstname" value="John" size="40">
<br>
Last name:<br>
<input type="text" name="lastname">
</form>
Try it yourself »

the maxlength Attribute

the maxlength attribute specifies the maximum allowed length for the input field:

Example

<form action="">
First name:<br>
<input type="text" name="firstname" maxlength="10">
<br>
Last name:<br>
<input type="text" name="lastname">
</form>
Try it yourself »

With a maxlength attribute, the input control will not accept more than the allowed number of characters.

the attribute does not provide any feedback. If you want to alert the user, you must write JavaScript code.

Note Input restrictions are not foolproof. JavaScript provides many ways to add illegal input.
To safely restrict input, restrictions must be checked by the receiver (the server) as well.


HTML5 Attributes

HTML5 added the following attributes for <input>:

and the following attributes for <form>:


the autocomplete Attribute

the autocomplete attribute specifies whether a form or input field should have autocomplete on or off.

When autocomplete is on, the browser automatically complete values based on values that the user has entered before.

Tip: It is possible to have autocomplete "on" for the form, and "off" for specific input fields, or vice versa.

the autocomplete attribute works with <form> and the following <input> types: text, search, url, tel, email, password, datepickers, range, and color.

Opera Safari ChromeFirefox Internet Explorer

Example

An HTML form with autocomplete on (and off for one input field):

<form action="action_page.php" autocomplete="on">
 First name:<input type="text" name="fname"><br>
 Last name: <input type="text" name="lname"><br>
 E-mail: <input type="email" name="email" autocomplete="off"> <br>
 <input type="submit">
</form>
Try it yourself »

Tip: In some browsers you may need to activate the autocomplete function for this to work.


the novalidate Attribute

the novalidate attribute is a <form> attribute.

When present, novalidate specifies that form data should not be validated when submitted.

Opera Safari ChromeFirefox Internet Explorer

Example

Indicates that the form is not to be validated on submit:

<form action="action_page.php" novalidate>
  E-mail: <input type="email" name="user_email">
  <input type="submit">
</form>
Try it yourself »

the autofocus Attribute

the autofocus attribute is a boolean attribute.

When present, it specifies that an <input> element should automatically get focus when the page loads.

OperaSafari ChromeFirefox Internet Explorer

Example

Let the "First name" input field automatically get focus when the page loads:

First name:<input type="text" name="fname" autofocus>
Try it yourself »

the form Attribute

the form attribute specifies one or more forms an <input> element belongs to.

Tip: To refer to more than one form, use a space-separated list of form ids.

Opera Safari Chrome Firefox Internet Explorer

Example

An input field located outside the HTML form (but still a part of the form):

<form action="action_page.php" id="form1">
  First name: <input type="text" name="fname"><br>
  <input type="submit" value="Submit">
</form>

Last name: <input type="text" name="lname" form="form1">
Try it yourself »

the formaction Attribute

the formaction attribute specifies the URL of a file that will process the input control when the form is submitted.

the formaction attribute overrides the action attribute of the <form> element.

the formaction attribute is used with type="submit" and type="image".

Opera Safari Chrome Firefox Internet Explorer

Example

An HTML form with two submit buttons, with different actions:

<form action="action_page.php">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br>
  <input type="submit" value="Submit"><br>
  <input type="submit" formaction="demo_admin.asp"
  value="Submit as admin">
</form>
Try it yourself »

the formenctype Attribute

the formenctype attribute specifies how the form-data should be encoded when submitting it to the server (only for forms with method="post").

the formenctype attribute overrides the enctype attribute of the <form> element.

the formenctype attribute is used with type="submit" and type="image".

Opera Safari Chrome Firefox Internet Explorer

Example

Send form-data that is default encoded (the first submit button), and encoded as "multipart/form-data" (the second submit button):

<form action="demo_post_enctype.asp" method="post">
  First name: <input type="text" name="fname"><br>
  <input type="submit" value="Submit">
  <input type="submit" formenctype="multipart/form-data"
  value="Submit as Multipart/form-data">
</form>
Try it yourself »

the formmethod Attribute

the formmethod attribute defines the HTTP method for sending form-data to the action URL.

the formmethod attribute overrides the method attribute of the <form> element.

the formmethod attribute can be used with type="submit" and type="image".

Opera Safari Chrome Firefox Internet Explorer

Example

the second submit button overrides the HTTP method of the form:

<form action="action_page.php" method="get">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br>
  <input type="submit" value="Submit">
  <input type="submit" formmethod="post" formaction="demo_post.asp"
  value="Submit using POST">
</form>
Try it yourself »

the formnovalidate Attribute

the novalidate attribute is a boolean attribute.

When present, it specifies that the <input> element should not be validated when submitted.

the formnovalidate attribute overrides the novalidate attribute of the <form> element.

the formnovalidate attribute can be used with type="submit".

Opera Safari Chrome Firefox Internet Explorer

Example

A form with two submit buttons (with and without validation):

<form action="action_page.php">
  E-mail: <input type="email" name="userid"><br>
  <input type="submit" value="Submit"><br>
  <input type="submit" formnovalidate value="Submit without validation">
</form>
Try it yourself »

the formtarget Attribute

the formtarget attribute specifies a name or a keyword that indicates where to display the response that is received after submitting the form.

the formtarget attribute overrides the target attribute of the <form> element.

the formtarget attribute can be used with type="submit" and type="image".

Opera Safari ChromeFirefox Internet Explorer

Example

A form with two submit buttons, with different target windows:

<form action="action_page.php">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br>
  <input type="submit" value="Submit as normal">
  <input type="submit" formtarget="_blank"
  value="Submit to a new window">
</form>
Try it yourself »

the height and width Attributes

the height and width attributes specify the height and width of an <input> element.

the height and width attributes are only used with <input type="image">.

Note Always specify the size of images. If the browser does not know the size, the page will flicker while images load.

Opera SafariChrome Firefox Internet Explorer

Example

Define an image as the submit button, with height and width attributes:

<input type="image" src="img_submit.gif" alt="Submit" width="48" height="48">
Try it yourself »

the list Attribute

the list attribute refers to a <datalist> element that contains pre-defined options for an <input> element.

Opera Safari Chrome Firefox Internet Explorer

Example

An <input> element with pre-defined values in a <datalist>:

<input list="browsers">

<datalist id="browsers">
  <option value="Internet Explorer">
  <option value="Firefox">
  <option value="Chrome">
  <option value="Opera">
  <option value="Safari">
</datalist>
Try it yourself »

the min and max Attributes

the min and max attributes specify the minimum and maximum value for an <input> element.

the min and max attributes work with the following input types: number, range, date, datetime, datetime-local, month, time and week.

Opera Safari Chrome Firefox Internet Explorer

Example

<input> elements with min and max values:

Enter a date before 1980-01-01:
<input type="date" name="bday" max="1979-12-31">

Enter a date after 2000-01-01:
<input type="date" name="bday" min="2000-01-02">

Quantity (between 1 and 5):
<input type="number" name="quantity" min="1" max="5">
Try it yourself »

the multiple Attribute

the multiple attribute is a boolean attribute.

When present, it specifies that the user is allowed to enter more than one value in the <input> element.

the multiple attribute works with the following input types: email, and file.

Opera Safari ChromeFirefox Internet Explorer

Example

A file upload field that accepts multiple values:

Select images: <input type="file" name="img" multiple>
Try it yourself »

the pattern Attribute

the pattern attribute specifies a regular expression that the <input> element's value is checked against.

the pattern attribute works with the following input types: text, search, url, tel, email, and password.

Tip: Use the global title attribute to describe the pattern to help the user.

Tip: Learn more about regular expressions in our JavaScript tutorial.

Opera Safari Chrome Firefox Internet Explorer

Example

An input field that can contain only three letters (no numbers or special characters):

Country code: <input type="text" name="country_code" pattern="[A-Za-z]{3}" title="three letter country code">
Try it yourself »

the placeholder Attribute

the placeholder attribute specifies a hint that describes the expected value of an input field (a sample value or a short description of the format).

the hint is displayed in the input field before the user enters a value.

the placeholder attribute works with the following input types: text, search, url, tel, email, and password.

Opera Safari Chrome Firefox Internet Explorer

Example

An input field with a placeholder text:

<input type="text" name="fname" placeholder="First name">
Try it yourself »

the required Attribute

the required attribute is a boolean attribute.

When present, it specifies that an input field must be filled out before submitting the form.

the required attribute works with the following input types: text, search, url, tel, email, password, date pickers, number, checkbox, radio, and file.

Opera Safari Chrome Firefox Internet Explorer

Example

A required input field:

Username: <input type="text" name="usrname" required>
Try it yourself »

the step Attribute

the step attribute specifies the legal number intervals for an <input> element.

Example: if step="3", legal numbers could be -3, 0, 3, 6, etc.

Tip: the step attribute can be used together with the max and min attributes to create a range of legal values.

the step attribute works with the following input types: number, range, date, datetime, datetime-local, month, time and week.

Opera SafariChrome Firefox Internet Explorer

Example

An input field with a specified legal number intervals:

<input type="number" name="points" step="3">
Try it yourself »