



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




















HTML forms are one of the most important parts of a website. They allow users to enter information and send it to a website or server. For example, when you create an account, log in to a website, search for something, place an order, or contact a company, you usually interact with an HTML form. Forms can collect many different types of information, such as: Name Email address Password Phone number Date Gender Address Comments File uploads Choices and preferences HTML provides different form elements and input types to collect this information in an organized way.
An HTML form is a section of a webpage that allows users to enter and submit data. The basic form is created using the element.
A form usually contains input fields, labels, buttons, and other controls.
Name:
Submit
In this example: creates the form. describes the input. creates an input field. creates a button.
The element is the container for form controls. It can contain different elements such as:
Username:
Submit
Important: Using POST does not by itself make data secure. Sensitive information should be sent over HTTPS.
The element is one of the most commonly used form elements in HTML. It allows users to enter different types of information. The type of input is controlled by the type attribute.
There are many different input types.
The text input creates a single-line text field. Full Name:
Users can enter information such as: Name Username City Job title
The password input is used for passwords.
Password:
Characters entered into a password field are normally hidden from view. Example:
The email input is designed for email addresses. Email:
Browsers can perform basic validation to check whether the entered value looks like an email address. For example:
The required attribute makes the field mandatory.
The number input is used for numerical values. Age:
You can also define a minimum and maximum value.
Date of Birth:
The browser usually provides a date picker. Other useful date/time input types include:
Radio buttons allow users to select one option from a group. Example:
Gender:
Male
Female The important part is that both radio buttons use the same name : name="gender" This groups them together so that the user can select only one option.
Checkboxes allow users to select one or more options. Example:
Skills:
HTML
CSS
JavaScript Unlike radio buttons, multiple checkboxes can be selected.
The file input allows users to select a file from their device. Upload Resume:
It can be used for: Resumes Images Documents Profile pictures Other files Example:
The accept attribute can help limit the types of files offered for selection.
The color input allows users to choose a color.
A more flexible option is: Submit The element is commonly preferred because it can contain richer content and is more flexible.
A reset button returns form controls to their initial values. Reset Example:
Clear
The element provides a description for a form control. Example: Email Address:
The for attribute of the label should match the id of the input. Here:
for="email" matches: id="email" This creates a relationship between the label and input.
Labels: Make forms easier to understand. Improve accessibility. Help screen readers identify form controls. Allow users to click the label to focus or select the associated control. For example:
I agree to the terms. Clicking the label can also select the checkbox.
The placeholder attribute displays a temporary hint inside an input. Example:
Another example:
A placeholder should not normally replace a proper label. The label tells the user what the field is for, while the placeholder can provide an example or hint.
The pattern attribute allows a regular expression to define a required format. Example:
This pattern allows letters only. Another example:
This requires exactly five digits. The pattern attribute can be useful when a particular format is required.
The element creates a dropdown list. Example: Country:
The user can select an option from the list.
The Unlike a normal text input, a textarea can contain multiple lines.
The element creates a clickable button. Example: Submit There are three common button types:
Submit Submits the form.
Reset
Example:
Email:
Submit
The browser checks whether the entered value has a generally valid email format. For example: [email protected] looks like an email address, while: example does not satisfy normal email input validation.
Password fields can also use validation attributes. Password:
This requires the user to enter at least 8 characters.
For real applications, password rules should be designed carefully, and passwords should be handled securely on the server.
Here is a complete example that combines many concepts from this chapter.
Registration Form
Registration Form
Full Name:
Email:
Password:
Age:
Reset
This example demonstrates: Form Labels Text input Email input Password input Number input Radio buttons Dropdown Textarea Checkbox Required validation Minimum password length Submit button Reset button
Here is a useful summary of common HTML input types. Input Type Purpose text Single-line text password Password entry email (^) Email address number Numerical value tel Telephone number url Website URL date Date selection time Time selection month (^) Month selection
Input Type Purpose week (^) Week selection radio Select one option checkbox Select one or more options file Upload/select a file color Select a color range (^) Select a value using a slider hidden (^) Submit a hidden value submit Submit the form reset Reset the form button General-purpose button
When creating HTML forms, follow these practices:
Instead of:
prefer: Name:
Use:
for email addresses instead of: