mozilla

Revision 464765 of Forms in HTML

  • Revision slug: Web/Guide/HTML/Forms_in_HTML
  • Revision title: Forms in HTML
  • Revision id: 464765
  • Created:
  • Creator: teoli
  • Is current revision? No
  • Comment Web/HTML/Forms_in_HTML Web/Guide/HTML/Forms_in_HTML

Revision Content

{{gecko_minversion_header("2")}}

Summary

Form elements and attributes in HTML5 provide a greater degree of semantic mark-up than HTML4 and remove a great deal of the need for tedious scripting and styling that was required in HTML4. The forms features in HTML5 provide a better experience for users by making forms more consistent across different web sites and giving immediate feedback to the user about data entry. They also provide this experience to users who have scripting disabled in their browser.

This topic describes new or changed items that are supported by Gecko/Firefox, version 4 or newer.

The <input> element

The {{HTMLElement("input")}} element has new values for the {{htmlattrxref("type", "input")}} attribute.

  • search: The element represents search entry field. Line breaks are automatically stripped from the input value, but no other syntax is enforced.
  • tel: The element represents a control for editing a telephone number. Line breaks are automatically stripped from the input value, but no other syntax is enforced, because telephone numbers vary widely internationally. You can use attributes such as {{htmlattrxref("pattern", "input")}} and {{htmlattrxref("maxlength", "input")}} to restrict values entered in the control.
  • url: The element represents a control for editing a URL. Line breaks and leading and trailing whitespaces are automatically stripped from the input value.
  • email: The element represents one email address. Line breaks are automatically stripped from the input value. An invalid email address can be set, but the input field will only satisfy its constraints if the email address satisfies the ABNF production 1*( atext / "." ) "@" ldh-str 1*( "." ldh-str ) where atext is defined in RFC 5322 section 3.2.3, and ldh-str is defined in RFC 1034 section 3.5.

    Note: If the {{htmlattrxref("multiple", "input")}} attribute is set, multiple email addresses may be entered into that {{HTMLElement("input")}} field, as a comma-separated list, but this is currently unimplemented in Firefox.

The {{HTMLElement("input")}} element also has new attributes:

  • {{htmlattrxref("list", "input")}}: The ID of a {{HTMLElement("datalist")}} element whose content, {{HTMLElement("option")}} elements, are to be used as hints and are displayed as proposals in the suggestion area of the input field.
  • {{htmlattrxref("pattern", "input")}}: A regular expression that the control's value is checked against, which can be used with {{htmlattrxref("type", "input")}} values of text, tel, search, url, and email.
  • {{htmlattrxref("form", "input")}}: A string indicating which {{HTMLElement("form")}} element this input is part of. An input can only be in one form.
  • {{htmlattrxref("formmethod", "input")}}: A string indicating which HTTP method (GET or POST) should be used when submitting; it overrides the {{htmlattrxref("method", "form")}} of the {{HTMLElement("form")}} element, if defined. The {{htmlattrxref("formmethod", "input")}} only applies when the {{htmlattrxref("type", "input")}} is image or submit, and the {{htmlattrxref("form", "input")}} attribute has been set.
  • {{htmlattrxref("x-moz-errormessage", "input")}} {{non-standard_inline}}: A string that is displayed as an error message when the field fails to validate. This is a Mozilla extension, and is non-standard.

The <form> element

The {{HTMLElement("form")}} element has a new attribute:

  • {{htmlattrxref("novalidate", "form")}}: This attribute prevents the form from being validated before its submission.

The <datalist> element

The {{HTMLElement("datalist")}} element represents the list of {{HTMLElement("option")}} elements to suggest when filling an {{HTMLElement("input")}} field.

You can use the {{htmlattrxref("list", "input")}} attribute on an {{HTMLElement("input")}} element to link a specific input field with a specific {{HTMLElement("datalist")}} element.

The <output> element

The {{HTMLElement("output")}} element represents the result of a calculation.

You can use the {{htmlattrxref("for", "output")}} attribute to specify a relationship between the {{HTMLElement("output")}} element and other elements in the document that affected the calculation (for example, as inputs or parameters). The value of the {{htmlattrxref("for", "output")}} attribute is a space-separated list of IDs of other elements.

{{non-standard_inline}} Gecko 2.0 (but not necessarily other browser engines) supports defining custom validity constraints and error messages for {{HTMLElement("output")}} elements, and therefore applies the {{Cssxref(":invalid")}}, {{Cssxref(":valid")}}, {{Cssxref(":-moz-ui-invalid")}}, and {{Cssxref(":-moz-ui-valid")}} CSS pseudo-classes to them. This can be helpful in situations where the calculated result violates a business rule, but no specific input value does (for example, "The total of percentages must not exceed 100").

The placeholder attribute

The {{htmlattrxref("placeholder", "input")}} attribute on {{HTMLElement("input")}} and {{HTMLElement("textarea")}} elements provides a hint to the user of what can be entered in the field. The placeholder text must not contain carriage returns or line-feeds.

The autofocus attribute

The {{htmlattrxref("autofocus", "input")}} attribute lets you specify that a form control should have input focus when the page loads, unless the user overrides it, for example by typing in a different control. Only one form item in a document can have the autofocus attribute, which is a Boolean. This attribute can be applied to the {{HTMLElement("input")}}, {{HTMLElement("button")}}, {{HTMLElement("select")}}, and {{HTMLElement("textarea")}} elements. The exception is that autofocus cannot be applied to an {{htmlattrxref("autofocus", "input")}} element if the {{htmlattrxref("type", "input")}} attribute is set to hidden (that is, you cannot automatically set focus to a hidden control).

The label.control DOM property

The HTMLLabelElement DOM interface provides an extra property, in addition to the properties that correspond to the HTML {{HTMLElement("label")}} element attributes. The control property returns the labeled control, that is, the control that the label is for, which is determined by the {{htmlattrxref("for", "label")}} attribute (if it is defined) or by the first descendant control element.

Constraint Validation

HTML5 provides syntax and API items to support client-side validation of forms. While this functionality does not replace server-side validation, which is still necessary for security and data integrity, client-side validation can support a better user experience by giving the user immediate feedback about the input data.

If the title attribute is set on the {{HTMLElement("input")}} element, its value is used as tooltip. However, if the validation fail, this tooltip text will be replaced with the associate error message. It's possible to customize that error message using the non-standard {{htmlattrxref("x-moz-errormessage")}} attribute or when calling the setCustomValidity() method.

<input type="email" title="Please, provide an e-mail" x-moz-errormessage="This is not a valid e-mail">
Note: Constraint validation is not supported on {{HTMLElement("button")}} elements in a form; to style a button based on the validity of the associated form, use the {{cssxref(":-moz-submit-invalid")}} pseudo-class.

HTML Syntax for Constraint Validation

The following items in HTML5 syntax can be used to specify constraints on form data.

  • The {{htmlattrxref("required", "input")}} attribute on the {{HTMLElement("input")}}, {{HTMLElement("select")}}, and {{HTMLElement("textarea")}} elements indicates that a value must be supplied. (On the {{HTMLElement("input")}} element, {{htmlattrxref("required", "input")}} applies only in conjunction with certain values of the {{htmlattrxref("type", "input")}} attribute.)
  • The {{htmlattrxref("pattern", "input")}} attribute on the {{HTMLElement("input")}} element constrains the value to match a specific regular expression.
  • The {{htmlattrxref("min", "input")}} and {{htmlattrxref("max", "input")}} attributes of the {{HTMLElement("input")}} element constrain the minimum and maximum values that can be entered.
  • The {{htmlattrxref("step", "input")}} attribute of the {{HTMLElement("input")}} element (when used in combination with the {{htmlattrxref("min", "input")}} and {{htmlattrxref("max", "input")}} attributes) constrains the granularity of values that can be entered. A value that does not correspond an allowed value step does not validate.
  • The {{htmlattrxref("maxlength", "input")}} attribute of the {{HTMLElement("input")}} and {{HTMLElement("textarea")}} elements constrains the maximum number of characters (in Unicode code points) that the user can enter.
  • The values url and email for the {{htmlattrxref("type", "input")}} constrain the value to being a valid URL or e-mail address, respectively.

In addition, you can prevent constraint validation by specifying the {{htmlattrxref("novalidate", "form")}} attribute on the {{HTMLElement("form")}}, or the {{htmlattrxref("formnovalidate", "button")}} attribute on the {{HTMLElement("button")}} element and on the {{HTMLElement("input")}} element (when {{htmlattrxref("type", "input")}} is submit or image). These attributes indicate that the form is not to be validated when it is submitted.

Constraint Validation API

The following DOM properties and methods related to constraint validation are available to client-side scripts:

  • On HTMLFormElement objects, the checkValidity() method, which returns true if all of this form element's associated elements that are subject to constraint validation satisfy their constraints, and false if any do not.
  • On form-associated elements:
    • willValidate property, which is false if the element has constraints that are not satisfied.
    • validity property, which is a ValidityState object representing the validity states that the element is in (i.e., constraint failure or success conditions).
    • validationMessage property, which is a message describing any constraint failures that pertain to the element.
    • checkValidity() method, which returns false if the element fails to satisfy any of its constraints, or true otherwise.
    • setCustomValidity() method, which sets a custom validation message, allowing for constraints to be imposed and validated beyond those that are predefined.

See also

Revision Source

<div>
  {{gecko_minversion_header("2")}}</div>
<h2 id="Summary">Summary</h2>
<p>Form elements and attributes in <strong>HTML5</strong> provide a greater degree of semantic mark-up than HTML4 and remove a great deal of the need for tedious scripting and styling that was required in HTML4. The forms features in HTML5 provide a better experience for users by making forms more consistent across different web sites and giving immediate feedback to the user about data entry. They also provide this experience to users who have scripting disabled in their browser.</p>
<p>This topic describes new or changed items that are supported by Gecko/Firefox, version 4 or newer.</p>
<h2 id="The_input_element" name="The_input_element">The <code>&lt;input&gt;</code> element</h2>
<p>The <code>{{HTMLElement("input")}}</code> element has new values for the {{htmlattrxref("type", "input")}} attribute.</p>
<ul>
  <li><code>search</code>: The element represents search entry field. Line breaks are automatically stripped from the input value, but no other syntax is enforced.</li>
  <li><code>tel</code>: The element represents a control for editing a telephone number. Line breaks are automatically stripped from the input value, but no other syntax is enforced, because telephone numbers vary widely internationally. You can use attributes such as {{htmlattrxref("pattern", "input")}} and {{htmlattrxref("maxlength", "input")}} to restrict values entered in the control.</li>
  <li><code>url</code>: The element represents a control for editing a <a class="external" href="http://en.wikipedia.org/URL" title="http://en.wikipedia.org/wiki/URL">URL</a>. Line breaks and leading and trailing whitespaces are automatically stripped from the input value.</li>
  <li>
    <p><code>email</code>: The element represents one email address. Line breaks are automatically stripped from the input value. An invalid email address can be set, but the input field will only satisfy its constraints if the email address satisfies the ABNF production <code>1*( atext / "." ) "@" ldh-str 1*( "." ldh-str )</code> where <code>atext</code> is defined in <a class="external" href="http://tools.ietf.org/html/rfc5322#section-3.2.3">RFC 5322 section 3.2.3</a>, and <code>ldh-str</code> is defined in <a class="external" href="http://tools.ietf.org/html/rfc1034#section-3.5">RFC 1034 section 3.5</a>.</p>
    <div class="note">
      <strong>Note:</strong> If the {{htmlattrxref("multiple", "input")}} attribute is set, multiple email addresses may be entered into that {{HTMLElement("input")}} field, as a comma-separated list, but this is currently unimplemented in Firefox.</div>
  </li>
</ul>
<p>The {{HTMLElement("input")}} element also has new attributes:</p>
<ul>
  <li>{{htmlattrxref("list", "input")}}: The ID of a {{HTMLElement("datalist")}} element whose content, {{HTMLElement("option")}} elements, are to be used as hints and are displayed as proposals in the suggestion area of the input field.</li>
  <li>{{htmlattrxref("pattern", "input")}}: A regular expression that the control's value is checked against, which can be used with {{htmlattrxref("type", "input")}} values of <code>text</code>, <code>tel</code>, <code>search</code>, <code>url</code>, and <code>email</code>.</li>
  <li>{{htmlattrxref("form", "input")}}: A string indicating which {{HTMLElement("form")}} element this input is part of. An input can only be in one form.</li>
  <li>{{htmlattrxref("formmethod", "input")}}: A string indicating which HTTP method (GET or POST) should be used when submitting; it overrides the {{htmlattrxref("method", "form")}} of the {{HTMLElement("form")}} element, if defined. The {{htmlattrxref("formmethod", "input")}} only applies when the {{htmlattrxref("type", "input")}} is <code>image</code> or <code>submit, and the </code>{{htmlattrxref("form", "input")}} attribute has been set.</li>
  <li>{{htmlattrxref("x-moz-errormessage", "input")}} {{non-standard_inline}}: A string that is displayed as an error message when the field fails to validate. This is a Mozilla extension, and is non-standard.</li>
</ul>
<h2 id="The_form_element" name="The_form_element">The <code>&lt;form&gt;</code> element</h2>
<p>The {{HTMLElement("form")}} element has a new attribute:</p>
<ul>
  <li>{{htmlattrxref("novalidate", "form")}}: This attribute prevents the form from being validated before its submission.</li>
</ul>
<h2 id="The_datalist_element" name="The_datalist_element">The <code>&lt;datalist&gt; </code>element</h2>
<p>The {{HTMLElement("datalist")}} element represents the list of {{HTMLElement("option")}} elements to suggest when filling an {{HTMLElement("input")}} field.</p>
<p>You can use the {{htmlattrxref("list", "input")}} attribute on an {{HTMLElement("input")}} element to link a specific input field with a specific {{HTMLElement("datalist")}} element.</p>
<h2 id="The_output_element" name="The_output_element">The <code>&lt;output&gt;</code> element</h2>
<p>The <code>{{HTMLElement("output")}}</code> element represents the result of a calculation.</p>
<p>You can use the {{htmlattrxref("for", "output")}} attribute to specify a relationship between the <code>{{HTMLElement("output")}}</code> element and other elements in the document that affected the calculation (for example, as inputs or parameters). The value of the {{htmlattrxref("for", "output")}} attribute is a space-separated list of IDs of other elements.</p>
<p>{{non-standard_inline}} Gecko 2.0 (but not necessarily other browser engines) supports defining custom validity constraints and error messages for {{HTMLElement("output")}} elements, and therefore applies the {{Cssxref(":invalid")}}, {{Cssxref(":valid")}}, {{Cssxref(":-moz-ui-invalid")}}, and {{Cssxref(":-moz-ui-valid")}} CSS pseudo-classes to them. This can be helpful in situations where the calculated result violates a business rule, but no specific input value does (for example, "The total of percentages must not exceed 100").</p>
<h2 id="The_placeholder_attribute" name="The_placeholder_attribute">The placeholder attribute</h2>
<p>The {{htmlattrxref("placeholder", "input")}} attribute on <code>{{HTMLElement("input")}}</code> and <code>{{HTMLElement("textarea")}}</code> elements provides a hint to the user of what can be entered in the field. The placeholder text must not contain carriage returns or line-feeds.</p>
<h2 id="The_autofocus_attribute" name="The_autofocus_attribute">The autofocus attribute</h2>
<p>The {{htmlattrxref("autofocus", "input")}} attribute lets you specify that a form control should have input focus when the page loads, unless the user overrides it, for example by typing in a different control. Only one form item in a document can have the <code>autofocus</code> attribute, which is a Boolean. This attribute can be applied to the <code>{{HTMLElement("input")}}</code>, <code>{{HTMLElement("button")}}</code>, <code>{{HTMLElement("select")}}</code>, and <code>{{HTMLElement("textarea")}}</code> <!-- and <code>keygen</code> -->elements. The exception is that <code>autofocus</code> cannot be applied to an {{htmlattrxref("autofocus", "input")}} element if the {{htmlattrxref("type", "input")}} attribute is set to <code>hidden</code> (that is, you cannot automatically set focus to a hidden control).</p>
<h2 id="The_label.control_DOM_property" name="The_label.control_DOM_property">The label.control DOM property</h2>
<p>The <code><a href="/en-US/docs/DOM/HTMLLabelElement" title="DOM/HTMLLabelElement">HTMLLabelElement</a></code> DOM interface provides an extra property, in addition to the properties that correspond to the HTML <code>{{HTMLElement("label")}}</code> element attributes. The <strong>control</strong> property returns the labeled control, that is, the control that the label is for, which is determined by the {{htmlattrxref("for", "label")}} attribute (if it is defined) or by the first descendant control element.</p>
<h2 id="Constraint_Validation" name="Constraint_Validation">Constraint Validation</h2>
<p>HTML5 provides syntax and API items to support client-side validation of forms. While this functionality does not replace server-side validation, which is still necessary for security and data integrity, client-side validation can support a better user experience by giving the user immediate feedback about the input data.</p>
<p>If the <code>title</code> attribute is set on the {{HTMLElement("input")}} element, its value is used as tooltip. However, if the validation fail, this tooltip text will be replaced with the associate error message. It's possible to customize that error message using the non-standard {{htmlattrxref("x-moz-errormessage")}} attribute or when calling the <code>setCustomValidity()</code> method.</p>
<pre class="brush: html">
&lt;input type="email" title="Please, provide an e-mail" x-moz-errormessage="This is not a valid e-mail"&gt;</pre>
<div class="note">
  <strong>Note:</strong> Constraint validation is not supported on {{HTMLElement("button")}} elements in a form; to style a button based on the validity of the associated form, use the {{cssxref(":-moz-submit-invalid")}} pseudo-class.</div>
<h3 id="HTML_Syntax_for_Constraint_Validation" name="HTML_Syntax_for_Constraint_Validation">HTML Syntax for Constraint Validation</h3>
<p>The following items in HTML5 syntax can be used to specify constraints on form data.</p>
<ul>
  <li>The {{htmlattrxref("required", "input")}} attribute on the {{HTMLElement("input")}}, {{HTMLElement("select")}}, and {{HTMLElement("textarea")}} elements indicates that a value must be supplied. (On the {{HTMLElement("input")}} element, {{htmlattrxref("required", "input")}} applies only in conjunction with certain values of the {{htmlattrxref("type", "input")}} attribute.)</li>
  <li>The {{htmlattrxref("pattern", "input")}} attribute on the {{HTMLElement("input")}} element constrains the value to match a specific regular expression.</li>
  <li>The {{htmlattrxref("min", "input")}} and {{htmlattrxref("max", "input")}} attributes of the {{HTMLElement("input")}} element constrain the minimum and maximum values that can be entered.</li>
  <li>The {{htmlattrxref("step", "input")}} attribute of the {{HTMLElement("input")}} element (when used in combination with the {{htmlattrxref("min", "input")}} and {{htmlattrxref("max", "input")}} attributes) constrains the granularity of values that can be entered. A value that does not correspond an allowed value step does not validate.</li>
  <li>The {{htmlattrxref("maxlength", "input")}} attribute of the {{HTMLElement("input")}} and {{HTMLElement("textarea")}} elements constrains the maximum number of characters (in Unicode code points) that the user can enter.</li>
  <li>The values <code>url</code> and <code>email</code> for the {{htmlattrxref("type", "input")}} constrain the value to being a valid URL or e-mail address, respectively.</li>
</ul>
<p>In addition, you can prevent constraint validation by specifying the {{htmlattrxref("novalidate", "form")}} attribute on the {{HTMLElement("form")}}, or the {{htmlattrxref("formnovalidate", "button")}} attribute on the {{HTMLElement("button")}} element and on the {{HTMLElement("input")}} element (when {{htmlattrxref("type", "input")}} is <code>submit</code> or <code>image</code>). These attributes indicate that the form is not to be validated when it is submitted.</p>
<h3 id="Constraint_Validation_API" name="Constraint_Validation_API">Constraint Validation API</h3>
<p>The following DOM properties and methods related to constraint validation are available to client-side scripts:</p>
<ul>
  <li>On <code><a href="/en-US/docs/DOM/HTMLFormElement" title="DOM/HTMLFormElement">HTMLFormElement</a></code> objects, the <code>checkValidity()</code> method, which returns true if all of this form element's associated elements that are subject to constraint validation satisfy their constraints, and false if any do not.</li>
  <li>On <a href="/en-US/docs/HTML/Content_categories#Form-associated" title="HTML/Content Categories#form-associated">form-associated elements</a>:
    <ul>
      <li><code>willValidate</code> property, which is false if the element has constraints that are not satisfied.</li>
      <li><code>validity</code> property, which is a <code><a href="/en-US/docs/DOM/ValidityState" title="DOM/ValidityState Interface">ValidityState</a></code> object representing the validity states that the element is in (i.e., constraint failure or success conditions).</li>
      <li><code>validationMessage</code> property, which is a message describing any constraint failures that pertain to the element.</li>
      <li><code>checkValidity()</code> method, which returns false if the element fails to satisfy any of its constraints, or true otherwise.</li>
      <li><code>setCustomValidity()</code> method, which sets a custom validation message, allowing for constraints to be imposed and validated beyond those that are predefined.</li>
    </ul>
  </li>
</ul>
<h2 id="See_also">See also</h2>
<ul>
  <li><a href="/en-US/docs/HTML/Forms" title="/en-US/docs/HTML/Forms">HTML Forms Guide</a></li>
</ul>
Revert to this revision