- HTML Forms
- Example
- The Element
- The Element
- Text Fields
- Example
- The Element
- Radio Buttons
- Example
- Checkboxes
- Example
- The Submit Button
- Example
- Example
- HTML Input Types
- HTML Input Types
- Input Type Text
- Example
- Input Type Password
- Example
- Input Type Submit
- Example
- Example
- Input Type Reset
- Example
- Input Type Radio
- Example
- Input Type Checkbox
- Example
- Input Type Button
- Example
- Input Type Color
- Example
- Input Type Date
- Example
- Example
- Input Type Datetime-local
- Example
- Input Type Email
- Example
- Input Type Image
- Example
- Input Type File
- Example
- Input Type Hidden
- Example
- Input Type Month
- Example
- Input Type Number
- Example
- Input Restrictions
- Example
- Input Type Range
- Example
- Input Type Search
- Example
- Input Type Tel
- Example
- Input Type Time
- Example
- Input Type Url
- Example
- Input Type Week
HTML Forms
An HTML form is used to collect user input. The user input is most often sent to a server for processing.
Example
The Element
The HTML element is used to create an HTML form for user input:
The element is a container for different types of input elements, such as: text fields, checkboxes, radio buttons, submit buttons, etc.
All the different form elements are covered in this chapter: HTML Form Elements.
The Element
The HTML element is the most used form element.
An element can be displayed in many ways, depending on the type attribute.
Type | Description |
---|---|
Displays a single-line text input field | |
Displays a radio button (for selecting one of many choices) | |
Displays a checkbox (for selecting zero or more of many choices) | |
Displays a submit button (for submitting the form) | |
Displays a clickable button |
All the different input types are covered in this chapter: HTML Input Types.
Text Fields
The defines a single-line input field for text input.
Example
A form with input fields for text:
This is how the HTML code above will be displayed in a browser:
Note: The form itself is not visible. Also note that the default width of an input field is 20 characters.
The Element
Notice the use of the element in the example above.
The tag defines a label for many form elements.
The element is useful for screen-reader users, because the screen-reader will read out loud the label when the user focuses on the input element.
The element also helps users who have difficulty clicking on very small regions (such as radio buttons or checkboxes) — because when the user clicks the text within the element, it toggles the radio button/checkbox.
The for attribute of the tag should be equal to the id attribute of the element to bind them together.
Radio Buttons
The defines a radio button.
Radio buttons let a user select ONE of a limited number of choices.
Example
A form with radio buttons:
Choose your favorite Web language:
This is how the HTML code above will be displayed in a browser:
Choose your favorite Web language:
Checkboxes
The defines a checkbox.
Checkboxes let a user select ZERO or MORE options of a limited number of choices.
Example
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
The Submit Button
The defines a button for submitting the form data to a form-handler.
The form-handler is typically a file on the server with a script for processing input data.
The form-handler is specified in the form’s action attribute.
Example
A form with a submit button:
This is how the HTML code above will be displayed in a browser:
The Name Attribute for
Notice that each input field must have a name attribute to be submitted.
If the name attribute is omitted, the value of the input field will not be sent at all.
Example
This example will not submit the value of the «First name» input field:
HTML Input Types
This chapter describes the different types for the HTML element.
HTML Input Types
Here are the different input types you can use in HTML:
Tip: The default value of the type attribute is «text».
Input Type Text
defines a single-line text input field:
Example
This is how the HTML code above will be displayed in a browser:
Input Type Password
defines a password field:
Example
This is how the HTML code above will be displayed in a browser:
The characters in a password field are masked (shown as asterisks or circles).
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
This is how the HTML code above will be displayed in a browser:
If you omit the submit button’s value attribute, the button will get a default text:
Example
Input Type Reset
defines a reset button that will reset all form values to their default values:
Example
This is how the HTML code above will be displayed in a browser:
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
defines a radio button.
Radio buttons let a user select ONLY ONE of a limited number of choices:
Example
Choose your favorite Web language:
This is how the HTML code above will be displayed in a browser:
Input Type Checkbox
defines a checkbox.
Checkboxes let a user select ZERO or MORE options of a limited number of choices.
Example
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
defines a button:
Example
This is how the HTML code above will be displayed in a browser:
Input Type Color
The 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
Input Type Date
The 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
You can also use the min and max attributes to add restrictions to dates:
Example
Input Type Datetime-local
The 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
Input Type Email
The 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
Input Type Image
The defines an image as a submit button.
The path to the image is specified in the src attribute.
Example
Input Type File
The defines a file-select field and a «Browse» button for file uploads.
Example
Input Type Hidden
The 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
Input Type Month
The allows the user to select a month and year.
Depending on browser support, a date picker can show up in the input field.
Example
Input Type Number
The 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
Input Restrictions
Here is a list of some common input restrictions:
Attribute | Description |
---|---|
checked | Specifies that an input field should be pre-selected when the page loads (for type=»checkbox» or type=»radio») |
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.
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
Input Type Range
The 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
Input Type Search
The is used for search fields (a search field behaves like a regular text field).
Example
Input Type Tel
The is used for input fields that should contain a telephone number.
Example
Input Type Time
The 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
Input Type Url
The 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
Input Type Week
The allows the user to select a week and year.
Depending on browser support, a date picker can show up in the input field.