HTML5 Form Attributes
New Form Attributes in HTML5
HTML5 has introduced several new attributes for the <form>
and <input>
tags.
New attributes for <form>
:
- autocomplete
- novalidate
New attributes for <input>
:
- autocomplete
- autofocus
- form
- formaction
- formenctype
- formmethod
- formnovalidate
- formtarget
- height and width
- list
- min and max
- multiple
- pattern (regexp)
- placeholder
- required
- step
<form>
/ <input>
autocomplete Attribute
The autocomplete attribute specifies whether a form or input field should have autocomplete enabled.
When a user starts to type in a field, the browser should display options to fill in the field, based on earlier typed values.
Tip: The autocomplete attribute can have a form where it is on, but within individual input elements, it can be disabled.
Note: Autocomplete works with the following <input>
types: text, search, url, telephone, email, password, datepickers, range, and color.
Example
Autocomplete on for the form, and off for one input field:
<form action="demo-form.php" autocomplete="on">
First name:<input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
E-mail: <input type="email" name="email" autocomplete="off"><br>
<input type="submit">
</form>
Tip: In some browsers, you may need to enable autocomplete functionality for this attribute to work.
<form>
novalidate Attribute
The novalidate attribute is a boolean attribute.
The novalidate attribute specifies that the form data should not be validated when submitted.
Example
Form data that will not be validated on submit:
<form action="demo-form.php" novalidate>
E-mail: <input type="email" name="user_email">
<input type="submit">
</form>
<input>
autofocus Attribute
The autofocus attribute is a boolean attribute.
The autofocus attribute specifies that the field should automatically get focus when the page loads.
Example
The "First name" input field automatically gets focus when the page loads:
<input>
form Attribute
The form attribute specifies one or more forms an input element belongs to.
Tip: To reference more than one form, use a space-separated list.
Example
An input field located outside of the HTML form (but still a part of it):
<input>
formaction Attribute
The formaction attribute describes the URL where the form data should be sent.
The formaction attribute overrides the action attribute of the <form>
element.
Note: The formaction attribute is used with type="submit" and type="image".
Example
A form with two submit buttons that post to different locations:
<input>
formenctype Attribute
The formenctype attribute describes how the form data should be encoded when submitted to the server (only for forms with method="post").
The formenctype attribute overrides the enctype attribute of the <form>
element.
Note: This attribute is used with type="submit" and type="image".
Example
The first submit button sends the form data with default encoding, the second with "multipart/form-data":
<input>
formmethod Attribute
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.
Note: This attribute is used with type="submit" and type="image".
Example
Redefining the form submission method:
<input>
formnovalidate Attribute
The formnovalidate attribute is a boolean attribute.
The formnovalidate attribute specifies that the form data should not be validated on submission.
The formnovalidate attribute overrides the novalidate attribute of the <form>
element.
Note: This attribute is used with type="submit".
Example
A form with two submit buttons, one with and one without validation:
<form action="demo-form.php">
E-mail: <input type="email" name="userid"><br>
<input type="submit" value="Submit"><br>
<input type="submit" formnovalidate value="Submit without validation">
</form>
<input>
formtarget Attribute
The formtarget attribute specifies a name or a keyword that indicates where to display the response that is received after submitting the form.
The formtarget attribute overrides the target attribute of the <form>
element.
Note: This attribute is used with type="submit" and type="image".
Example
A form with two submit buttons that display the response in different windows:
<form action="demo-form.php">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<input type="submit" value="Normal submit">
<input type="submit" formtarget="_blank" value="Submit to a new page">
</form>
<input>
height and width Attributes
The height and width attributes specify the height and width of an <input>
element of type image.
Note: The height and width attributes are only used with <input type="image">
.
Tip: It is good practice to always specify both the height and width for images. If height and width are set, the space required for the image is reserved when the page is loaded. However, without these attributes, the browser does not know the size of the image and cannot reserve the appropriate space to it. The effect will be that the page layout will change during loading (while the images load).
Example
Defines an image as a submit button, with height and width attributes:
<input>
list Attribute
The list attribute refers to a <datalist>
element that contains pre-defined options for an <input>
element.
Example
Predefined options in a <datalist>
for an <input>
field:
<input>
min and max Attributes
The min, max, and step attributes specify constraints for input elements containing numbers or dates.
Note: The min, max, and step attributes work with the following input types: date pickers, number, and range.
Example
Setting minimum and maximum values for an input element:
<input>
multiple Attribute
The multiple attribute is a boolean attribute.
The multiple attribute specifies that the user is allowed to enter more than one value in the <input>
element.
Note: The multiple attribute works with the following input types: email, and file.
Example
Upload multiple files:
<input>
pattern Attribute
The pattern attribute specifies a regular expression that the <input>
element's value is checked against.
Note: 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.
Tip: Learn more about regular expressions in our JavaScript tutorial.
Example
A text field that can contain only three letters (no numbers or special characters):
<input>
placeholder Attribute
The placeholder attribute provides a hint that describes the expected value of an input field.
A short hint is displayed in the input field before the user enters a value.
Note: The placeholder attribute works with the following input types: text, search, url, tel, email, and password.
Example
Input field with a placeholder text:
<input>
required Attribute
The required attribute is a boolean attribute.
The required attribute specifies that an input field must be filled out before submitting the form.
Note: The required attribute works with the following input types: text, search, url, tel, email, password, date pickers, number, checkbox, radio, and file.
Example
An input field that cannot be empty:
<input>
step Attribute
The step attribute specifies the legal number intervals for an input field.
If step="3", the 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.
Note: The step attribute works with the following input types: number, range, date, datetime, datetime-local, month, time, and week.
Example
Specifies the step interval for an input field:
HTML5 <input>
Tags
Tag | Description |
---|---|
<form> | Defines a form |
<input> | Defines an input field |