New HTML5 Form Elements – 10/2016

Here is a list of new form elements in HTML5:
You’re welcome!

<input>: as the most important element, can be displayed in several ways, depending on the type attribute. The input attributes are:

<input type=“text”> defines a one-line text input field.
<input type=“password”> defines a password field.
<input type=“submit”> defines a button for submitting form data to a form-handler.
<input type=“reset”> defines a reset button that will reset all form values to their default values.
<input type=“radio”> defines a radio button.
<input type=“checkbox”> defines a checkbox.
<input type=“button”> defines a button.
<input type=“number”> defines a numeric input field.You can also set restrictions on what numbers are accepted.

HTML5 added several new input types:

<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.
<input type=“datetime”> is a control for entering a data and time (hour, minute, second, and fraction of a second) based on the UTC time zone.
<input type=“datetime-local”> specifies a date and time input field, with no time zone.
<input type=“email”> is used for input fields that should contain an e-mail address. Some smartphones recognize the email type, and adds “.com” to the keyboard to match email input.
<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.
<input type=“number”> defines a numeric input field. You can also set restrictions on what numbers are accepted.
<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.
<input type=“search”> is used for search fields (a search field behaves like a regular text field).
<input type=“tel”> is used for input fields that should contain a telephone number.
<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.
<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.
<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.
<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.

The form-handler is specified in the form’s action attribute. The form-handler is typically a server page with a script for processing input data.

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

<option> elements defines an option that can be selected, and is the default for the first item in a drop-down list.
<textarea> element defines a multi-line input field (a text area). The rows attribute specifies the visible number of lines in a text area. The cols attribute specifies the visible width of a text area.
<button> element defines a clickable button.

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

<output> element represents the result of a calculation (like one performed by a script). Example: Perform a calculation and show the result in an <output> element:

<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>
<input type=”submit”>
</form>

Input Restrictions

Here is a list of some common input restrictions (some are new in HTML5):

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 value attribute specifies the initial value for an input field:

The readonly attribute specifies that the input field is read only (cannot be changed).

<form action=””>
First name:
<input type=”text” name=”firstname” value=”John” readonly>
</form>

The disabled attribute specifies that the input field is disabled. A disabled input field is unusable and un-clickable, and its value will not be sent when submitting the form.

<form action=””>
First name:
<input type=”text” name=”firstname” value=”John” disabled>
</form>

The size attribute specifies the size (in characters) for the input field.

<form action=””> First name:
<input type=”text” name=”firstname” value=”John” size=”40″>
</form>

The maxlength attribute specifies the maximum allowed length for the input field.

<form action=””>
First name:
<input type=”text” name=”firstname” maxlength=”10″>
</form>

HTML5 Attributes

The autocomplete attribute specifies whether a form or input field should have autocomplete on or off.

When autocomplete is on, the browser automatically complete the input values based on values that the user has entered before.

Tip: It is possible to have autocomplete “on” for the form, and “off” for specific input fields, or vice versa.

The autocomplete attribute works with </form> and the following <input> types: text, search, url, tel, email, password, datepickers, range, and color.

<form action=”action_page.php” autocomplete=”on”>
First name:<input type=”text” name=”fname”>
Last name: <input type=”text” name=”lname”>
E-mail: <input type=”email” name=”email” autocomplete=”off”>
<input type=”submit”>
</form>

The autofocus attribute specifies that the input field should automatically get focus when the page loads.

First name:<input type=”text” name=”fname” autofocus>

The form attribute specifies one or more forms an <input> element belongs to. Tip: To refer to more than one form, use a space-separated list of form ids.

<form action=”action_page.php” id=”form1″>
First name: <input type=”text” name=”fname”>
<input type=”submit” value=”Submit”>
</form>

The formaction attribute specifies the URL of a file that will process the input control when the form is submitted. The formaction attribute overrides the action attribute of the <form> element.

The formaction attribute is used with type=”submit” and type=”image”. An HTML form with two submit buttons, with different actions:

<form action=”action_page.php”>
First name: <input type=”text” name=”fname”>
Last name: <input type=”text” name=”lname”>
<input type=”submit” value=”Submit”>
<input type=”submit” formaction=”demo_admin.asp”
value=”Submit as admin”>
</form>

The formenctype attribute specifies how the form data should be encoded when submitted (only for forms with method=”post”e;). The formenctype attribute overrides the enctype attribute of the <form> element.</form>.

The formenctype attribute is used with type=”submit” and type=”image”. Send form-data that is default encoded (the first submit button), and encoded as “multipart/form-data” (the second submit button):

<form action=”demo_post_enctype.asp” method=”post”>
First name: <input type=”text” name=”fname”>
<input type=”submit” value=”Submit”>
<input type=”submit” formenctype=”multipart/form-data” value=”Submit as Multipart/form-data”>
</form>

The formmethod attribute defines the HTTP method for sending form-data to the action URL. The formmethod attribute overrides the method attribute of the <form> element.

The formmethod attribute can be used with type=”submit” and type=”image”. The second submit button overrides the HTTP method of the form:

<form action=”action_page.php” method=”get”>
First name: <input type=”text” name=”fname”>
Last name: <input type=”text” name=”lname”>
<input type=”submit” value=”Submit”>
<input type=”submit” formmethod=”post” formaction=”demo_post.asp”
value=”Submit using POST”>
</form>

The formnovalidate attribute overrides the novalidate attribute of the <form> element. The formnovalidate attribute can be used with type=”submit”.

<form action=”action_page.php”>
E-mail: <input type=”email” name=”userid”>
<input type=”submit” value=”Submit”>
<input type=”submit” formnovalidate value=”Submit without validation”>
</form>

The formtarget attribute specifies a name or a keyword that indicates where to display the response that is received after submitting the form. The formtargetattribute overrides the target attribute of the <form> element.

The formtarget attribute can be used with type=”submit” and type=”image”.

<form action=”action_page.php”>
First name: <input type=”text” name=”fname”>
Last name: <input type=”text” name=”lname”>
<input type=”submit” value=”Submit as normal”>
<input type=”submit” formtarget=”_blank”
value=”Submit to a new window”>
</form>

The height and width attributes specify the height and width of an <input type=”image”> element.

Always specify the size of images in a form. If the browser does not know the size, the page will flicker while images load.

Define an image as the submit button, with height and width attributes:

<input type=”image” src=”img_submit.gif” alt=”Submit” width=”48″ height=”48″>

The list attribute refers to a <datalist> element that contains pre-defined options for an <input> element. An <input> element with pre-defined values in a <datalist>:

<input list=”browsers”>
<datalist id=”browsers”>
<option value=”Internet Explorer”>
<option value=”Firefox”>
<option value=”Chrome”>
<option value=”Opera”>
<option value=”Safari”>
</datalist>

The min and max attributes specify the minimum and maximum values for an <input> element. The min and max attributes work with the following input types:number, range, date, datetime-local, month, time and week.

<input> elements with min and max values:
Enter a date before 1980-01-01:
<input type=”date” name=”bday” max=”1979-12-31″>
Enter a date after 2000-01-01:
<input type=”date” name=”bday” min=”2000-01-02″>
Quantity (between 1 and 5):
<input type=”number” name=”quantity” min=”1″ max=”5″>

The multiple attribute specifies that the user is allowed to enter more than one value in the <input> element. The multiple attribute works with the following input types: email, and file. A file upload field that accepts multiple values:

Select images: <input type=”file” name=”img” multiple>

The pattern attribute specifies a regular expression that the <input> element’s value is checked against. The pattern attribute works with the following input types: text, search, url, tel, email, and password.

Tip: Use the global title attribute to describe the pattern to help the user.

An input field that can contain only three letters (no numbers or special characters):
Country code: <input type=”text” name=”country_code” pattern=”[A-Za-z]{3}” title=”Three letter country code”>

The placeholder attribute specifies a hint that describes the expected value of an input field (a sample value or a short description of the format). The hint is displayed in the input field before the user enters a value. The placeholder attribute works with the following input types: text, search, url, tel, email, and password.

<input type=”text” name=”fname” placeholder=”First name”>

The required attribute specifies that an input field must be filled out before submitting the form. The required attribute works with the following input types: text, search, url, tel, email, password, date pickers, number, checkbox, radio, and file.

Username: <input type=”text” name=”usrname” required>

The step attribute specifies the legal number intervals for an <input> element. Example: if step=”3″, legal numbers could be -3, 0, 3, 6, etc.

Tip: The step attribute can be used together with the max and min attributes to create a range of legal values.

The step attribute works with the following input types: number, range, date, datetime-local, month, time and week.

<input type=”number” name=”points” step=”3″>

This seamless list of information was gathered from the W3C website.