HTML Form
HTML Form
ABHISHEK SINGH
05/28/2024 1
HTML Forms
The <form> Element
The HTML <form> element defines a form that is used to collect user input:
<form>
.
<!-- form elements
Form elements are different types of input elements, like text fields,
checkboxes, radio buttons, submit buttons, and more.
-->
.
</form>
HTML Forms (Attributes )
The Action Attribute
The action attribute defines the action to be performed when the form is
submitted. Normally, the form data is sent to a web page on the server when the user
clicks on the submit button.
<form action="action_page.php">
If the action attribute is omitted, the action is set to the current page.
HTML Forms (Attributes )
The Method Attribute
The method attribute specifies the HTTP method (GET or POST) to be used when
submitting the form data:
action_page.php?firstname=Mickey&lastname=Mouse
action_page.php
HTML Forms (Attributes )
The Name Attribute
Each input field must have a name attribute to be submitted.
If the name attribute is omitted, the data of that input field will not be sent at all.
This example will only submit the "Last name" input field:
Example
<form action="action_page.php">
First name:<br>
<input type="text" value="Mickey"><br>
Last name:<br>
<input type="text" name="lastname" value="Mouse">
<br><br>
<input type="submit" value="Submit">
</form>
HTML Forms (Attributes )
Grouping Form Data with <fieldset>
The <fieldset> element is used to group related data in a form.
The <legend> element defines a caption for the <fieldset> element.
Example
<form action="action_page.php">
<fieldset>
<legend>Personal information:</legend>
First name:<br>
<input type="text" name="firstname" value="Mickey"><br>
Last name:<br>
<input type="text" name="lastname" value="Mouse"><br>
<br>
<input type="submit" value="Submit">
</fieldset>
</form>
HTML Forms (Attributes )
Attribute Description
accept-charset Specifies the charset used in the submitted form (default:
the page charset).
action Specifies an address (url) where to submit the form
(default: the submitting page).
autocomplete Specifies if the browser should autocomplete the form
(default: on).
enctype Specifies the encoding of the submitted data (default: is
url-encoded).
method Specifies the HTTP method used when submitting the
form (default: GET).
name Specifies a name used to identify the form (for DOM
usage: document.forms.name).
novalidate Specifies that the browser should not validate the form.
Example
<form>
First name:
<br>
<input type="text“ name="firstname“ ><br>
Last name:
<br>
<input type="text" name="lastname">
</form>
HTML Form Elements
HTML Input Types
Input Type Password
Example
<form>
First name:
<br>
<input type="text“ name="firstname“ >
<br>
Password:
<br>
<input type=“password" name=“password">
</form>
HTML Form Elements
HTML Input Types
Input Type Submit
Example
<form action="action_page.php">
First name:<br>
<input type="text" name="firstname" value="Mickey"><br>
Last name:<br>
<input type="text" name="lastname" value="Mouse"><br><br>
<input type="submit" value="Submit">
</form>
HTML Form Elements
HTML Input Types
Input Type Reset
<input type="reset"> defines a reset button that will reset all form values to
their default values:
Example
<form action="action_page.php">
First name:<br>
<input type="text" name="firstname" value="Mickey"><br>
Last name:<br>
<input type="text" name="lastname" value="Mouse"><br><br>
<input type="submit" value="Submit">
<input type="reset">
</form>
HTML Form Elements
HTML Input Types
Example
<form>
</form>
HTML Form Elements
HTML Input Types
Input Type Checkbox
Example
<form>
</form>
HTML Form Elements
HTML Input Types
Input Type Button
<input type="button"> defines a button:
Example
<form>
</form>
HTML Form Elements
HTML 5 Input Types
<form>
Quantity:
</form>
HTML Form Elements
HTML 5 Input Types
Input Type Date
The <input type="date"> is used for input fields that should contain a date.
Depending on browser support, a date picker can show up in the input field.
<form>
Birthday:
<input type="date" name="bday">
</form>
<form>
Enter a date before 1980-01-01:
<input type="date" name="bday" max="1979-12-31"><br>
Enter a date after 2000-01-01:
<input type="date" name="bday" min="2000-01-02"><br>
</form>
HTML Form Elements
HTML 5 Input Types
Input Type Color
The <input type="color"> is used for input fields that should contain a color.
Depending on browser support, a color picker can show up in the input field.
<form>
</form>
Example
The <input type="range"> is used for input fields that should contain a value
within a range.
Depending on browser support, the input field can be displayed as a slider
control.
<form>
</form>
HTML Form Elements
HTML 5 Input Types
Input Type Month
The <input type="month"> allows the user to select a month and year.
Depending on browser support, a date picker can show up in the input field.
<form>
</form>
HTML Form Elements
HTML 5 Input Types
Input Type Week
The <input type="week"> allows the user to select a week and year.
Depending on browser support, a date picker can show up in the input field.
<form>
Select a week:
<input type="week" name="week_year">
</form>
HTML Form Elements
HTML 5 Input Types
Input Type Time
The <input type="time"> allows the user to select a time (no time zone).
Depending on browser support, a time picker can show up in the input field.
<form>
Select a week:
<input type=“time" name=“user time">
</form>
HTML Form Elements
HTML 5 Input Types
Input Type Datetime-local
The <input type="datetime-local"> 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.
<form>
</form>
HTML Form Elements
HTML 5 Input Types
Input Type Email
The <input type="email"> 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 adds ".com" to the keyboard to
match email input.
<form>
E-mail:
<input type="email" name="email">
</form>
HTML Form Elements
HTML 5 Input Types
<form>
Search Google:
<input type="search" name="googlesearch">
</form>
HTML Form Elements
HTML 5 Input Types
Input Type Tel
The <input type="tel"> is used for input fields that should contain a telephone
number.
The tel type is currently supported only in Safari 8.
<form>
Telephone:
<input type="tel" name="usrtel">
</form>
HTML Form Elements
HTML 5 Input Types
<form>
Add your homepage:
<input type="url" name="homepage">
</form>
HTML Form Elements
HTML Input Types(Input Restriction)
Attribute Description
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
HTML Form Elements
HTML 5 Input Types(Input Attributes Restriction)
The readonly attribute specifies that the input field is read only (cannot be changed):
Example
<form action="">
First name:<br>
<input type="text" name="firstname" value="John" readonly>
</form>
HTML Form Elements
HTML 5 Input Types(Input Attributes Restriction)
Example
<form action="">
First name:<br>
<input type="text" name="firstname" value="John" disabled>
</form>
HTML Form Elements
HTML 5 Input Types(Input Attributes Restriction)
The size attribute specifies the size (in characters) for the input field:
<form action="">
First name:<br>
<input type="text" name="firstname" value="John" size="40">
</form>
HTML Form Elements
HTML 5 Input Types(Input Attributes Restriction)
The maxlength attribute specifies the maximum allowed length for input
field.
<form action="">
First name:<br>
<input type="text" name="firstname" value="John" maxlength="40">
</form>
THANKS
05/28/2024 37