0% found this document useful (0 votes)
43 views

HTML Form Elements and Input Types

The document describes different HTML form elements and input types. It details the <input>, <select>, <textarea>, <button>, and other elements. It also covers new HTML5 form elements like <datalist>, <keygen>, and <output>. The document provides examples and explanations of common input types like text, password, radio buttons, checkboxes and more.

Uploaded by

jeffrey
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

HTML Form Elements and Input Types

The document describes different HTML form elements and input types. It details the <input>, <select>, <textarea>, <button>, and other elements. It also covers new HTML5 form elements like <datalist>, <keygen>, and <output>. The document provides examples and explanations of common input types like text, password, radio buttons, checkboxes and more.

Uploaded by

jeffrey
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

HTML Form Elements

The <input> Element


- The most important form element is the <input> element. The <input>
element can be displayed in several ways, depending on the type attribute.
The <select> Element
- The <select> element defines a drop-down list.

<select name="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>

- The <option> elements defines an option that can be selected. By default,


the first item in the drop-down list is selected. To define a pre-selected option,
add the selected attribute to the option:

<option value="fiat" selected>Fiat</option>


The <textarea> Element
- The <textarea> element defines a multi-line input field (a text area):

<textarea name="message" rows="10" cols="30">


The cat was playing in the garden.
</textarea>

The rows attribute specifies the visible number of lines in a text area.
The cols attribute specifies the visible width of a text area.
The <button> Element
- The <button> element defines a clickable button:

<button type="button" onclick="alert('Hello World!')">Click Me!</button>

HTML5 Form Elements


HTML5 <datalist> Element
- The <datalist> element specifies a list of pre-defined options for an
<input> element. Users will see a drop-down list of the pre-defined options
as they input data. The list attribute of the <input> element, must refer to
the id attribute of the <datalist> element.

<form action="action_page.php">
<input list="browsers">
<datalist id="browsers">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
</form>
HTML5 <keygen> Element
- The purpose of the <keygen> element is to provide a secure way to
authenticate users. The <keygen> element specifies a key-pair generator
field in a form. When the form is submitted, two keys are generated, one
private and one public. The private key is stored locally, and the public key is
sent to the server. The public key could be used to generate a client
certificate to authenticate the user in the future.

<form action="action_page.php">
Username: <input type="text" name="user">
Encryption: <keygen name="security">
<input type="submit">
</form>
HTML5 <output> Element
- The <output> element represents the result of a calculation (like one
performed by a script).
<form action="action_page.php"
oninput="x.value=parseInt(a.value)+parseInt(b.value)">
0
<input type="range" id="a" name="a" value="50">
100 +
<input type="number" id="b" name="b" value="50">
=
<output name="x" for="a b"></output>
<br><br>
<input type="submit">
</form>

HTML Input Types


Input Type Text
- <input type="text"> defines a one-line text input field:

<form>
First name:<br>
<input type="text" name="firstname"><br>
Last name:<br>
<input type="text" name="lastname">
</form>
Input Type Password
- <input type="password"> defines a password field:

<form>
User name:<br>
<input type="text" name="username"><br>
User password:<br>
<input type="password" name="psw">
</form>
Input Type Submit
- <input type="submit"> defines a button for submitting form data to a form-
handler. The form-handler is typically a server page with a script for
processing input data. The form-handler is specified in the form's action
attribute:

<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>

- If you omit the submit button's value attribute, the button will get a default
text:

<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">
</form>
Input Type Reset
- <input type="reset"> defines a reset button that will reset all form values
to their default values:

<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>

- If you change the input values and then click the "Reset" button, the form-
data will be reset to the default values.
Input Type Radio
- <input type="radio"> defines a radio button. Radio buttons let a user select
ONLY ONE of a limited number of choices:

<form>
<input type="radio" name="gender" value="male" checked> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="other"> Other
</form>
Input Type Checkbox
- <input type="checkbox"> defines a checkbox. Checkboxes let a user select
ZERO or MORE options of a limited number of choices.

<form>
<input type="checkbox" name="vehicle1" value="Bike"> I have a
bike<br>
<input type="checkbox" name="vehicle2" value="Car"> I have a car
</form>
Input Type Button
- <input type="button"> defines a button:

<input type="button" onclick="alert('Hello World!')" value="Click Me!">

HTML5 Input Types


Note: New input types that are not supported by older web browsers will behave as
<input type="text">.

Input Type Number


- The <input type="number"> defines a numeric input field. You can also set
restrictions on what numbers are accepted. The following example displays a
numeric input field, where you can enter a value from 1 to 5:
<form>
Quantity (between 1 and 5):
<input type="number" name="quantity" min="1" max="5">
</form>

Input Restrictions
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

- The following example displays a numeric input field, where you can enter a
value from 0 to 100, in steps of 10. The default value is 30:

<form>
Quantity:
<input type="number" name="points" min="0" max="100" step="10"
value="30">
</form>
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>

- You can also add restrictions to dates:

<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>
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>
Select your favorite color:
<input type="color" name="favcolor">
</form>
Input Type Range
- 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>
<input type="range" name="points" min="0" max="10">
</form>

- You can use the following attributes to specify restrictions: min, max, step,
value.
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>
Birthday (month and year):
<input type="month" name="bdaymonth">
</form>
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>
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 time:
<input type="time" name="usr_time">
</form>
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>
Birthday (date and time):
<input type="datetime-local" name="bdaytime">
</form>
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>
Input Type Search
- The <input type="search"> is used for search fields (a search field behaves
like a regular text field).

<form>
Search Google:
<input type="search" name="googlesearch">
</form>
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>
Input Type Url
- The <input type="url"> is used for input fields that should contain a URL
address. Depending on browser support, the url field can be automatically
validated when submitted. Some smartphones recognize the url type, and
adds ".com" to the keyboard to match url input.

<form>
Add your homepage:
<input type="url" name="homepage">
</form>

You might also like