forms
forms
HTML form basics include the common HTML elements, tags, attributes,
concepts, or best practices required for you to create good HTML forms.
The collected data is sent to a server for processing.
Form Validation
Conclusion
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<button type="submit">Submit</button>
</form>
The HTML <form> element is a container for several HTML form
elements. The <form> element can contain the following:
<input>
<label>
<select>
<textarea>
<button>
<fieldset>
<legend>
<datalist>
<output>
<option>
<optgroup>
The <input> element can only accept a particular type of data assigned
to it using the <type> attribute.
<form action="">
<input
type="text"
id="name"
name="username"
placeholder="Enter your username"
required
/>
<input
type="password"
id="security"
name="password"
placeholder="Enter your password"
required
/>
<input type="email" name="email" placeholder="Enter your email" />
<input type="checkbox" name="subscribe" value="yes" /> Subscribe to t
newsletter <input type="radio" name="gender" value="male" /> Male
<input type="radio" name="gender" value="female" /> Female
<input type="submit" name="submit" />
</form>
The following are the different <type> attributes that can be assigned
to an <input> element:
The following are other possible attributes found in the input element:
<input name=" "> : Assigns the input field a name. The assigned
name identifies the input data when the form is submitted.
<input id=" "> : The identifier creates a unique id for the input
field. It is usually associated with CSS for styling and JavaScript
for other manipulations.
<input value=" "> : Used to set the initial value for the input
field. The default initial value gives the user an idea of the
information required.
<input readonly> : The user can only read the initially set value
but can't change it. Unlike the disabled attribute, the input field is
clickable but can't be modified.
The label element describes the information required in the text field.
The label element is important for accessibility, this makes it easier for
screen-reader users to navigate the form. The assistive technologies
read the label loud to users.
Clicking on the label focuses the corresponding input field when the
for attribute of the label element corresponds with the id attribute of
the input element, making it more convenient for users to interact with
the form.
Label improves the overall usage of the form, providing context and
guidance.
The following are the commonly used attributes for the label element:
<form action="">
<label for="testimony">Testimony:</label>
<textarea name="testimony" id="testimony" cols="30" rows="10"></texta
</form>
The rows attribute controls the height (vertical size) of the textarea box,
determining the number of visible lines while the cols attribute
controls the width (horizontal size), specifying the number of visible
characters per line.
Note, that the textarea box can wrap to fit the entered text within its
defined width.
Unlike the single-line input field, the textarea element does not have a
maxlength attribute or value attribute. The content is placed within
the opening and closing tags.
<form action="">
<label for="numbers">Choose a favorite number:</label>
<select name="numbers" id="numbers" size="5" multiple>
<option value="" disabled selected>Select a number</option>
<option value="one">1</option>
<option value="two">2</option>
<option value="three">3</option>
<option value="four">4</option>
<option value="five">5</option>
<option value="six">6</option>
<option value="seven">7</option>
<option value="eight">8</option>
<option value="nine">9</option>
<option value="ten">10</option>
</select>
</form>
The <option> element is contained within the <select> element. The
<option> element holds the items to be selected. Each <option>
represents one item in the drop-down list.
The <name=" "> attribute identifies the select control on the server side
when the form is submitted. The <name=" "> is important for processing
the form data on the server.
You can select one of the options as the default selection by including
the <selected> attribute in the <option> element. If no option is
selected, then the first option in the list will be selected by default.
The <size=" "> attribute sets the number of options you can see at
once in the drop-down by setting the <size=" "> attribute on the
<select> . Note that other options are seen as you scroll down.
Length Limits
You can use the maxlength attribute to set the maximum number of
characters an input field can hold.
<form>
<input type="text" id="username" name="username" placeholder="Username" m
<button type="submit">Submit</button>
</form>
Required Fields
Requires that certain input fields are filled before the form is submitted.
The <required> attribute is used to perform this technique. An error
message is displayed when the required field is not filled by the user.
<form>
<input type="text" id="username" name="username" placeholder="Username" r
<input type="email" id="email" name="email" placeholder="Email" required>
<button type="submit">Submit</button>
</form>
Data Format
Ensures the data entered by the user follows the required format. The
<input type="email"> type attribute set to email will require the user
to enter the correct email format (for example: [email protected])
before form submission.
The same thing happens if the type attribute is set to number <input
type="number"> , the user can only put data from 0-9.
<form>
<input type="email" id="email" name="email" placeholder="Email" required>
<button type="submit">Submit</button>
</form>
Password Strength
The <pattern=""> attribute is used to specify password complexity
such as minimum length, and the inclusion of uppercase or lowercase
letters, numbers, and special characters.
The <title=""> attribute displays the error message when the user
hovers over the input field or when the entered password does not
match the specified password format. The higher the password
complexity, the higher the user account is protected from unauthorized
access.
<form>
<input type="password" id="password" name="password" placeholder="Passwor
<button type="submit">Submit</button>
</form>
Numeric values
You can set the range of numeric values to be entered by the user by
using the min and max attributes. For example, to check if a user is
within the specified age range:
<form>
<input type="number" id="age" name="age" placeholder="Age" min="18" max="
<button type="submit">Submit</button>
</form>
GET Method
With the get method <method="get"> , the form data is sent using the
URL in the browser's address bar to the server.
Using the above code sample, when the user enters a name (let's say the
user's name is KC) in the input field named ''name'', and clicks the submit
button, the form data will be sent to the server in the URL like this:
"http://example.com?name=KC".
The GET method is not safe, as it is commonly used for sending small
amounts of data that is not sensitive.
POST Method
The post method attribute <method=post> sends the form data in the
body of the HTTP request to the server, rather than in the URL.
Using the same code sample above, the POST method will send the form
data to the server like this: "https://example.com/submit.php".
You'd should notice that the POST request does not contain the form
data in the URL but rather points to the server-side script (submit.php)
that will process the form data.
The sent form data is not visible. The POST request is used to submit
sensitive information, like passwords, since the data is not visible in the
URL, but rather sent in the HTTP body request.
Make sure your input fields have the appropriate type attributes.
Use CSS media queries to adjust your form layouts and styles
based on the viewport size.
Conclusion
Creating good HTML forms that meet web standards improve user
interaction and experience on your website. By following the best
practices and accessibility, developers can create forms that are user-
friendly, and effective in collecting data from users.
You can learn more about HTML forms and responsive web design using
freeCodeCamp's Responsive Web Design Certification
Kelechukwu Isaac Awoke
Read more posts.
ADVERTISEMENT
Our mission: to help people learn to code for free. We accomplish this by creating thousands of
videos, articles, and interactive coding lessons - all freely available to the public.
Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services,
and staff.
Trending Guides
Date Formatting in JS Java Iterator Hashmap Cancel a Merge in Git
What is a Linked List? Install Java in Ubuntu Python Ternary Operator
Full Stack Career Guide Python Sort Dict by Key Smart Quotes Copy/Paste
JavaScript Array Length Sets in Python Kotlin vs Java
SQL Temp Table HTML Form Basics Comments in YAML
Pandas Count Rows Python End Program Python XOR Operator
Python Dict Has Key Python List to String Exit Function in Python
String to Array in Java Python Import from File Parse a String in Python
Python Merge Dictionaries Copy a Directory in Linux Reactive Programming Guide
Center Text Vertically CSS What’s a Greedy Algorithm? Edit Commit Messages in Git
Mobile App
Our Charity
About Alumni Network Open Source Shop Support Sponsors Academic Honesty