Love your Forms
In the 20/20 section I didn't venture to include any form features. Forms represent the all important 'call to action' for a page. Historically, putting forms together was often a fairly dreary job for a web designer. However, recent enhancements in HTML have made this process more creative and it is a very important part of web design as it is often how your website makes its cash!
HTML forms are used to collect user input. The form data is then sent to a server for processing. This tutorial covers the core features of HTML forms.
Basic Form Structure
A form in HTML is created using the <form>
element. Here's a simple form example:
<form>
: The container for form elements.action
: Specifies the URL where the form data is sent.method
: Specifies the HTTP method (GET or POST).
Input Types
HTML provides various input types to collect different kinds of data:
Text Input
Password Input
Email Input
Number Input
Date Input
Form Controls
HTML forms include various controls for user input:
Checkboxes
Radio Buttons
Dropdown Lists (Select)
Text Area
Form Attributes
Forms have several important attributes:
action
: URL to send the form data.method
: HTTP method (GET, POST).autocomplete
: Enables or disables autocomplete.target
: Specifies where to display the response (e.g.,_blank
for new window).
Grouping Form Data
You can group form elements using the <fieldset>
and <legend>
elements:
Labels and Accessibility
Using elements enhances accessibility:
The for
attribute of the should match the
id
of the corresponding input element.
Form Validation
HTML5 provides built-in form validation:
- Required Fields: Use the
required
attribute.
- Pattern Matching: Use the
pattern
attribute to specify a regular expression.
- Input Length: Use
minlength
andmaxlength
.
Submit and Reset Buttons
Forms usually include a submit button to send data and a reset button to clear it:
Example Form
Here's a complete example of a form with various input types and validation:
Registration Form
More Examples
More examples can be found at:
HTML5 Form Element Demos