The Wayback Machine - https://web.archive.org/web/20240625183333/https://www.geeksforgeeks.org/html-forms/
Open In App

HTML Forms

Last Updated : 20 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

HTML Forms utilize the <form> element as a powerful tool to collect user input through a variety of interactive controls. These controls range from text fields, numeric inputs, email fields, password fields, to checkboxes, radio buttons, and submit buttons. In essence, an HTML Form serves as a versatile container for numerous input elements, thereby enhancing user interaction.

Syntax:

<form>
<!--form elements-->
</form>

Form Elements

The HTML <form> comprises several elements, each serving a unique purpose. For instance, the <label> element is used to define labels for other <form> elements. The <input> element, on the other hand, is versatile and can be used to capture various types of input data such as text, password, email, and more, simply by altering its type attribute. Now, let’s all the list of HTML Form Elements one by one:

Elements

Descriptions

<label>It defines labels for <form> elements.
<input>It is used to get input data from the form in various types such as text, password, email, etc by changing its type.
<button>It defines a clickable button to control other elements or execute a functionality.
<select>It is used to create a drop-down list.
<textarea>It is used to get input long text content.
<fieldset>It is used to draw a box around other form elements and group the related data.
<legend>It defines a caption for fieldset elements
<datalist>It is used to specify pre-defined list options for input controls.
<output>It displays the output of performed calculations.
<option>It is used to define options in a drop-down list.
<optgroup>It is used to define group-related options in a drop-down list.

Commonly Used Input Types in HTML Forms

In HTML forms, various input types are used to collect different types of data from users. Here are some commonly used input types:

<input type=”text”>

Input Type

Description

<input type=”text“>

Defines a one-line text input field

<input type=”password“>

Defines a password field

<input type=”submit“>

Defines a submit button

<input type=”reset“>

Defines a reset button

<input type=”radio“>

Defines a radio button

<input type=”email“>

Validates that the input is a valid email address.

<input type=”number“>

Allows the user to enter a number. You can specify min, max, and step attributes for range.

<input type=”checkbox“>

Used for checkboxes where the user can select multiple options.

<input type=”date“>

Allows the user to select a date from a calendar.

<input type=”time“>

Allows the user to select a time.

<input type=”file“>

Allows the user to select a file to upload.

HTML Forms Example

1. Basic HTML Forms Example:

Example: This HTML forms collects the user personal information such as username and password with the button to submit the form.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>Html Forms</title>
</head>
<body>
  <h2>HTML Forms</h2>
  <form>
    <label for="username">Username:</label><br>
    <input type="text" id="username" name="username"><br><br>
    
    <label for="password">Password:</label><br>
    <input type="password" id="password" name="password"><br><br>
    
    <input type="submit" value="Submit">
  </form>
</body> 
</html>

Output:


HTML Forms

Basic HTML Form


2. HTML Forms Example:

Example: This HTML form collects user personal information including name, email, password, gender, date of birth, and address. It features proper styling for input fields and submission button.

HTML
<!DOCTYPE html>
<html>
    <head>
        <title>HTML Form</title>
        <style>
            body {
                display: flex;
                justify-content: center;
                align-items: center;
                height: 100vh;
                margin: 0;
                background-color: #f0f0f0;
            }

            form {
                width: 400px;
                background-color: #fff;
                padding: 20px;
                border-radius: 8px;
                box-shadow: 0 0 10px
                    rgba(0, 0, 0, 0.1);
            }

            fieldset {
                border: 1px solid black;
                padding: 10px;
                margin: 0;
            }

            legend {
                font-weight: bold;
                margin-bottom: 10px;
            }

            label {
                display: block;
                margin-bottom: 5px;
            }

            input[type="text"],
            input[type="email"],
            input[type="password"],
            textarea,
            input[type="date"] {
                width: calc(100% - 20px);
                padding: 8px;
                margin-bottom: 10px;
                box-sizing: border-box;
                border: 1px solid #ccc;
                border-radius: 4px;
            }

            input[type="radio"] {
                margin-left: 20px;
            }

            input[type="submit"] {
                padding: 10px 20px;

                border-radius: 5px;
                cursor: pointer;
            }
        </style>
    </head>
    <body>
        <form>
            <fieldset>
                <legend>
                    User personal information
                </legend>
                <label
                    >Enter your full name</label
                >
                <input type="text" name="name" />
                <label>Enter your email</label>
                <input
                    type="email"
                    name="email"
                />
                <label>Enter your password</label>
                <input
                    type="password"
                    name="pass"
                />
                <label
                    >Confirm your password</label
                >
                <input
                    type="password"
                    name="confirmPass"
                />
                <label>Enter your gender</label>
                <input
                    type="radio"
                    name="gender"
                    value="male"
                />Male
                <input
                    type="radio"
                    name="gender"
                    value="female"
                />Female
                <input
                    type="radio"
                    name="gender"
                    value="others"
                />Others
                <label
                    >Enter your Date of
                    Birth</label
                >
                <input type="date" name="dob" />
                <label>Enter your Address:</label>
                <textarea
                    name="address"
                ></textarea>
                <input
                    type="submit"
                    value="submit"
                />
            </fieldset>
        </form>
    </body>
</html>

Output:

HTMLForm3

HTML Forms Example Output

Here are some of the key attributes that can be used with the <form> element:

  1. action: This attribute specifies where to send the form-data when a form is submitted. The value of this attribute is typically a URL.
  2. method: This attribute defines the HTTP method used to send the form-data. The values can be “get” or “post”.
  3. target: This attribute specifies where to display the response received after submitting the form. The values can be “_blank”, “_self”, “_parent”, “_top”, or the name of an iframe.
  4. enctype: This attribute is used when method=“post”. It specifies how the form-data should be encoded when submitting it to the server. The values can be “application/x-www-form-urlencoded”, “multipart/form-data”, or “text/plain”.
  5. autocomplete: This attribute specifies whether a form should have autocomplete on or off. When autocomplete is on, the browser automatically completes values based on values that the user has entered before.
  6. novalidate: This Boolean attribute specifies that the form-data should not be validated on submission.

Features

  • Facilitates user input collection through various elements.
  • Utilizes <form> tags to structure input elements.
  • Defines actions for data submission upon form completion.
  • Supports client-side validation for enhanced user experience.

Conclusion

In conclusion, HTML forms are a powerful tool for collecting user input on the web. They provide a wide range of interactive controls, from text fields to checkboxes, radio buttons, and more. Understanding the syntax and attributes of the <form> element is crucial for creating effective and user-friendly forms.

This article has provided a comprehensive overview of HTML forms, including their syntax, key attributes, and various form elements. By mastering HTML forms, you can create more engaging, user-friendly, and accessible web applications.



Previous Article
Next Article

Similar Reads

Spectre Forms Horizontal forms
Spectre Forms Horizontal forms are horizontal forms if you want to create a horizontal form then you have to use the form-horizontal class in form element. For each fields in the form you can use col-* and col-xs/sm/lg/xl-* class to the child elements for responsive form row layout. Here the * can be replaceable by 1 to 12 any number you want based
2 min read
Styling Django Forms with django-crispy-forms
Django by default doesn't provide any Django form styling method due to which it takes a lot of effort and precious time to beautifully style a form. django-crispy-forms solves this problem for us. It will let you control the rendering behavior of your Django forms in a very elegant and DRY way. Modules required:django : django installdjango-crispy
1 min read
Reactive Forms vs Template Driven Forms
Angular provides two main approaches for handling forms: Reactive Forms and Template-Driven Forms. In this article, we'll learn more about Reactive Forms and Template-Driven Forms in detail. Table of Content Reactive FormsTemplate Driven FormsDifference between Reactive Forms and Template Driven FormsConclusionReactive FormsReactive Forms is a way
6 min read
HTML DOM forms Collection
The DOM forms collection is used to return the collection of all &lt;form&gt; elements in a HTML document. The form elements are sorted as they appear in the source code. Syntax: document.forms Properties: It returns the number of form in the collection of elements. Methods: The DOM forms collection contains three methods which are listed below: [i
4 min read
How to disable spell checking from Input Box and Textarea in HTML forms?
Basically the concept of spell check feature is used when we enter the grammatically incorrect words inside &lt;input&gt; or &lt;textarea&gt; fields in HTML form you will see the red underline below the incorrect words. It is used to detect grammatical or spelling mistakes in the text fields. To disable spellcheck in an HTML form the spellcheck att
1 min read
How to specify multiple forms the select field belongs to in HTML ?
The task is to specify multiple forms the select field belongs to. In simple wording, we have to find out which form the specific select belongs to. You can achieve this task by using the form attribute. select element - It is used to create a drop-down list in HTML.form element - It is used to create a form for user input.form attribute - This att
2 min read
How do we take password input in HTML forms ?
In this article, we learn how to create a password input field in HTML. This can be done by using the &lt;input&gt; tag with type attribute which is set to the value "password". It involves sensitive information about the data of the user. The text in the input password field will be changed into bullets in the view of hiding from the third party.
1 min read
How to add a checkbox in forms using HTML ?
In this article, we will learn how to add a checkbox in HTML Forms. Basically, the checkbox is used to select one or more options from a variety of options. It is a multi-control unit that will be presented as a small square box on the screen. Normally, Checkbox has 3 states namely- Checked, unchecked &amp; indeterminate. It is required in forms wh
1 min read
How to prevent buttons from submitting forms in HTML ?
In this article, we will use different approaches for preventing buttons from submitting forms. There are several reasons where preventing the buttons from submitting forms, i.e., if the user hasn't completed all the required form fields, or added incorrect details in the form, etc. For this, we can implement different approaches, which are discuss
3 min read
Understanding HTML Form Encoding: URL Encoded and Multipart Forms
HTML forms contain two encodings, the URL Encoded Forms and the multipart Forms. The encoding in an HTML form is determined by an attribute named 'enctype'. This attribute can have three values: AttributeDescriptionapplication/x-www-form-urlencodedRepresents a URL-encoded form. Default enctype for sending form data.multipart/form-dataRepresents a m
3 min read
Article Tags :
three90RightbarBannerImg