From 4b1a9203c547c019fc5398082ae19a3f3d4c3efe Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:41:15 -0500 Subject: initial commit --- files/ar/web/html/element/input/index.html | 1380 ++++++++++++++++++++++ files/ar/web/html/element/input/radio/index.html | 356 ++++++ 2 files changed, 1736 insertions(+) create mode 100644 files/ar/web/html/element/input/index.html create mode 100644 files/ar/web/html/element/input/radio/index.html (limited to 'files/ar/web/html/element/input') diff --git a/files/ar/web/html/element/input/index.html b/files/ar/web/html/element/input/index.html new file mode 100644 index 0000000000..4d6726e1bf --- /dev/null +++ b/files/ar/web/html/element/input/index.html @@ -0,0 +1,1380 @@ +--- +title: ': The Input (Form Input) element' +slug: Web/HTML/Element/input +tags: + - Data entry + - Element + - Forms + - HTML + - HTML forms + - HTML input tag + - Input + - MakeBrowserAgnostic + - NeedsTranslation + - Reference + - TopicStub + - Web +translation_of: Web/HTML/Element/input +--- +
{{HTMLRef}}
+ +

The HTML <input> element is used to create interactive controls for web-based forms in order to accept data from the user; a wide variety of types of input data and control widgets are available, depending on the device and {{Glossary("user agent")}}. The <input> element is one of the most powerful and complex in all of HTML due to the sheer number of combinations of input types and attributes.

+ +
{{EmbedInteractiveExample("pages/tabbed/input-text.html", "tabbed-shorter")}}
+ + + +

<input> types

+ +

How an <input> works varies considerably depending on the value of its {{htmlattrxref("type", "input")}} attribute, hence the different types are covered in their own separate reference pages. If this attribute is not specified, the default type adopted is text.

+ +

The available types are as follows:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
TypeDescriptionBasic ExamplesSpec
{{HTMLElement("input/button", "button")}}A push button with no default behavior displaying the value of the {{anch('htmlattrdefvalue', 'value')}} attribute, empty by default. + + {{EmbedLiveSample("examplebutton",200,55,"","", "nobutton")}}
{{HTMLElement("input/checkbox", "checkbox")}}A check box allowing single values to be selected/deselected. + + {{EmbedLiveSample("examplecheckbox",200,55,"","", "nobutton")}}
{{HTMLElement("input/color", "color")}}A control for specifying a color; opening a color picker when active in supporting browsers. + + {{EmbedLiveSample("examplecolor",200,55,"","", "nobutton")}}{{HTMLVersionInline("5")}}
{{HTMLElement("input/date", "date")}}A control for entering a date (year, month, and day, with no time). Opens a date picker or numeric wheels for year, month, day when active in supporting browsers. + + {{EmbedLiveSample("exampledate",200,55,"","", "nobutton")}}{{HTMLVersionInline("5")}}
{{HTMLElement("input/datetime-local", "datetime-local")}}A control for entering a date and time, with no time zone. Opens a date picker or numeric wheels for date- and time-components when active in supporting browsers. + + {{EmbedLiveSample("exampledtl",200,55,"","", "nobutton")}}{{HTMLVersionInline("5")}}
{{HTMLElement("input/email", "email")}}A field for editing an email address. Looks like a text input, but has validation parameters and relevant keyboard in supporting browsers and devices with dynamic keyboards. + + {{EmbedLiveSample("exampleemail",200,55,"","", "nobutton")}}{{HTMLVersionInline("5")}}
{{HTMLElement("input/file", "file")}}A control that lets the user select a file. Use the {{anch('htmlattrdefaccept', 'accept')}} attribute to define the types of files that the control can select. + + {{EmbedLiveSample("examplefile",'100%',55,"","", "nobutton")}}
{{HTMLElement("input/hidden", "hidden")}}A control that is not displayed but whose value is submitted to the server. There is an example in the next column, but it's hidden!
{{HTMLElement("input/image", "image")}}A graphical submit button. Displays an image defined by the src attribute. The {{anch('htmlattrdefalt', 'alt')}} attribute displays if the image {{anch('htmlattrdefsrc', 'src')}} is missing. + + {{EmbedLiveSample("exampleimage",200,55,"","", "nobutton")}}
{{HTMLElement("input/month", "month")}}A control for entering a month and year, with no time zone. + + {{EmbedLiveSample("examplemonth",200,55,"","", "nobutton")}}{{HTMLVersionInline("5")}}
{{HTMLElement("input/number", "number")}}A control for entering a number. Displays a spinner and adds default validation when supported. Displays a numeric keypad in some devices with dynamic keypads. + + {{EmbedLiveSample("examplenumber",200,55,"","", "nobutton")}}{{HTMLVersionInline("5")}}
{{HTMLElement("input/password", "password")}}A single-line text field whose value is obscured. Will alert user if site is not secure. + + {{EmbedLiveSample("examplepassword",200,55,"","", "nobutton")}}
{{HTMLElement("input/radio", "radio")}}A radio button, allowing a single value to be selected out of multiple choices with the same {{anch('htmlattrdefname', 'name')}} value. + + {{EmbedLiveSample("exampleradio",200,55,"","", "nobutton")}}
{{HTMLElement("input/range", "range")}}A control for entering a number whose exact value is not important. Displays as a range widget defaulting to the middle value. Used in conjunction {{anch('htmlattrdefmin', 'min')}} and {{anch('htmlattrdefmax', 'max')}} to define the range of acceptable values. + + {{EmbedLiveSample("examplerange",200,55,"","", "nobutton")}}{{HTMLVersionInline("5")}}
{{HTMLElement("input/reset", "reset")}}A button that resets the contents of the form to default values. Not recommended. + + {{EmbedLiveSample("examplereset",200,55,"","", "nobutton")}}
{{HTMLElement("input/search", "search")}}A single-line text field for entering search strings. Line-breaks are automatically removed from the input value. May include a delete icon in supporting browsers that can be used to clear the field. Displays a search icon instead of enter key on some devices with dynamic keypads. + + {{EmbedLiveSample("examplesearch",200,55,"","", "nobutton")}}{{HTMLVersionInline("5")}}
{{HTMLElement("input/submit", "submit")}}A button that submits the form. + + {{EmbedLiveSample("examplesubmit",200,55,"","", "nobutton")}}
{{HTMLElement("input/tel", "tel")}}A control for entering a telephone number. Displays a telephone keypad in some devices with dynamic keypads. + + {{EmbedLiveSample("exampletel",200,55,"","", "nobutton")}}{{HTMLVersionInline("5")}}
{{HTMLElement("input/text", "text")}}The default value. A single-line text field. Line-breaks are automatically removed from the input value. + + {{EmbedLiveSample("exampletext",200,55,"","", "nobutton")}}
{{HTMLElement("input/time", "time")}}A control for entering a time value with no time zone. + + {{EmbedLiveSample("exampletime",200,55,"","", "nobutton")}}{{HTMLVersionInline("5")}}
{{HTMLElement("input/url", "url")}}A field for entering a URL. Looks like a text input, but has validation parameters and relevant keyboard in supporting browsers and devices with dynamic keyboards. + + {{EmbedLiveSample("exampleurl",200,55,"","", "nobutton")}}{{HTMLVersionInline("5")}}
{{HTMLElement("input/week", "week")}}A control for entering a date consisting of a week-year number and a week number with no time zone. + + {{EmbedLiveSample("exampleweek",200,55,"","", "nobutton")}}{{HTMLVersionInline("5")}}
Obsolete values
{{HTMLElement("input/datetime", "datetime")}}{{deprecated_inline}} {{obsolete_inline}} A control for entering a date and time (hour, minute, second, and fraction of a second) based on UTC time zone. + + {{EmbedLiveSample("exampledatetime",200,75,"","", "nobutton")}}{{HTMLVersionInline("5")}}
+ +

Attributes

+ +

The <input> element is so powerful because of its attributes; the {{htmlattrxref("type", "input")}} attribute, described with examples above, being the most important. Since every <input> element, regardless of type, is based on the {{domxref("HTMLInputElement")}} interface, they technically share the exact same set of attributes. However, in reality, most attributes have an effect on only a specific subset of input types. In addition, the way some attributes impact an input depends on the input type, impacting different input types in different ways.

+ +

This section provides a table listing all the attributes with a brief description. This table is followed by a list describing each attribute in greater detail, along with which input types they are associated with.Those that are common to most or all input types are defined in greater detail below. Attributes that are unique to particular input types—or attributes which are common to all input types but have special behaviors when used on a given input type—are instead documented on those types' pages. This element includes the global attributes. Those with extra importance as it relates to <input> are highlighted.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Attributes for the {{htmlelement('input')}} element include global HTML attributes and:
AttributeType or TypesDescription
{{anch('htmlattrdefaccept', 'accept')}}fileHint for expected file type in file upload controls
{{anch('htmlattrdefalt', 'alt')}}imagealt attribute for the image type. Required for accessibility
{{anch('htmlattrdefautocomplete', 'autocomplete')}}allHint for form autofill feature
{{anch('htmlattrdefautofocus', 'autofocus')}}allAutomatically focus the form control when the page is loaded
{{anch('htmlattrdefcapture', 'capture')}}fileMedia capture input method in file upload controls
{{anch('htmlattrdefchecked', 'checked')}}radio, checkboxWhether the command or control is checked
{{anch('htmlattrdefdirname', 'dirname')}}text, searchName of form field to use for sending the element's directionality in form submission
{{anch('htmlattrdefdisabled', 'disabled')}}allWhether the form control is disabled
{{anch('htmlattrdefform', 'form')}}allAssociates the control with a form element
{{anch('htmlattrdefformaction', 'formaction')}}image, submitURL to use for form submission
{{anch('htmlattrdefformenctype', 'formenctype')}}image, submitForm data set encoding type to use for form submission
{{anch('htmlattrdefformmethod', 'formmethod')}}image, submitHTTP method to use for form submission
{{anch('htmlattrdefformnovalidate', 'formnovalidate')}}image, submitBypass form control validation for form submission
{{anch('htmlattrdefformtarget', 'formtarget')}}image, submitBrowsing context for form submission
{{anch('htmlattrdefheight', 'height')}}imageSame as height attribute for {{htmlelement('img')}}; vertical dimension
{{anch('htmlattrdeflist', 'list')}}almost allValue of the id attribute of the {{htmlelement('datalist')}} of autocomplete options
{{anch('htmlattrdefmax', 'max')}}numeric typesMaximum value
{{anch('htmlattrdefmaxlength', 'maxlength')}}password, search, tel, text, urlMaximum length (number of characters) of value
{{anch('htmlattrdefmin', 'min')}}numeric typesMinimum value
{{anch('htmlattrdefminlength', 'minlength')}}password, search, tel, text, urlMinimum length (number of characters) of value
{{anch('htmlattrdefmultiple', 'multiple')}}email, fileBoolean. Whether to allow multiple values
{{anch('htmlattrdefname', 'name')}}allName of the form control. Submitted with the form as part of a name/value pair.
{{anch('htmlattrdefpattern', 'pattern')}}password, text, telPattern the value must match to be valid
{{anch('htmlattrdefplaceholder', 'placeholder')}}password, search, tel, text, urlText that appears in the form control when it has no value set
readonlyalmost allBoolean. The value is not editable
requiredalmost allBoolean. A value is required or must be check for the form to be submittable
{{anch('htmlattrdefsize', 'size')}}email, password, tel, textSize of the control
{{anch('htmlattrdefsrc', 'src')}}imageSame as src attribute for {{htmlelement('img')}}; address of image resource
{{anch('htmlattrdefstep', 'step')}}numeric typesIncremental values that are valid.
{{anch('htmlattrdeftype', 'type')}}allType of form control
{{anch('htmlattrdefvalue', 'value')}}allCurrent value of the form control. Submitted with the form as part of a name/value pair.
{{anch('htmlattrdefwidth', 'width')}}imageSame as width attribute for {{htmlelement('img')}}
+ +

A few additional non-standard attributes are listed following the descriptions of the standard attributes.

+ +

Individual attributes

+ +
+
{{htmlattrdef("accept")}}
+
+

Valid for the file input type only, the accept attribute defines which file types are selectable in a file upload control. See the {{HTMLElement("input/file", "file")}} input type.

+
+
{{htmlattrdef("alt")}}
+
+

Valid for the image button only, the alt attribute provides alternative text for the image, displaying the value of the attribute if the image src is missing or otherwise fails to load. See the {{HTMLElement("input/image", "image")}} input type.

+
+
{{htmlattrdef("autocomplete")}}
+
+

(Not a Boolean attribute!) The autocomplete attribute takes as its value a space-separated string that describes what, if any, type of autocomplete functionality the input should provide. A typical implementation of autocomplete simply recalls previous values entered in the same input field, but more complex forms of autocomplete can exist. For instance, a browser could integrate with a device's contacts list to autocomplete email addresses in an email input field. See {{SectionOnPage("/en-US/docs/Web/HTML/Attributes/autocomplete", "Values")}} for permitted values.

+ +

The autocomplete attribute is valid on hidden, text, search, url, tel, email, date, month, week, time, datetime-local, number, range, color, and password. This attribute has no effect on input types that do not return numeric or text data, being valid for all input types except checkbox, radio, file, or any of the button types.

+ +

See The HTML autocomplete attribute for additional information, including information on password security and how autocomplete is slightly different for hidden than for other input types.

+
+
{{htmlattrdef("autofocus")}}
+
+

A Boolean attribute which, if present, indicates that the input should automatically have focus when the page has finished loading (or when the {{HTMLElement("dialog")}} containing the element has been displayed).

+ +
+

Note: An element with the autofocus attribute may gain focus before the {{domxref("DOMContentLoaded")}} event is fired.

+
+ +

No more than one element in the document may have the autofocus attribute. If put on more than one element, the first one with the attribute receives focus.

+ +

The autofocus attribute cannot be used on inputs of type hidden, since hidden inputs cannot be focused.

+ +
+

Warning: Automatically focusing a form control can confuse visually-impaired people using screen-reading technology and people with cognitive impairments. When autofocus is assigned, screen-readers "teleport" their user to the form control without warning them beforehand.

+
+ +

Use careful consideration for accessibility when applying the autofocus attribute. Automatically focusing on a control can cause the page to scroll on load. The focus can also cause dynamic keyboards to display on some touch devices. While a screen reader will announce the label of the form control receiving focus, the screen reader will not announce anything before the label, and the sighted user on a small device will equally miss the context created by the preceding content.

+
+
{{htmlattrdef("capture")}}
+
+

Introduced in the HTML Media Capture specification and valid for the file input type only, the capture attribute defines which media—microphone, video, or camera—should be used to capture a new file for upload with file upload control in supporting scenarios. See the {{HTMLElement("input/file", "file")}} input type.

+
+
{{htmlattrdef("checked")}}
+
+

Valid for both radio and checkbox types, checked is a Boolean attribute. If present on a radio type, it indicates that that radio button is the currently selected one in the group of same-named radio buttons. If present on a checkbox type, it indicates that the checkbox is checked by default (when the page loads). It does not indicate whether this checkbox is currently checked: if the checkbox’s state is changed, this content attribute does not reflect the change. (Only the HTMLInputElement’s checked IDL attribute is updated.)

+ +
+

Note: Unlike other input controls, a checkboxes and radio buttons value are only included in the submitted data if they are currently checked. If they are, the name and the value(s) of the checked controls are submitted.

+ +

For example, if a checkbox whose name is fruit has a value of cherry, and the checkbox is checked, the form data submitted will include fruit=cherry. If the checkbox isn't active, it isn't listed in the form data at all. The default value for checkboxes and radio buttons is on.

+
+
+
{{htmlattrdef("dirname")}}
+
+

Valid for text and search input types only, the dirname attribute enables the submission of the directionality of the element. When included, the form control will submit with two name/value pairs: the first being the name and value, the second being the value of the dirname as the name with the value of ltr or rtl being set by the browser.

+ +
<form action="page.html" method="post">
+  <label>Fruit: <input type="text" name="fruit" dirname="fruit.dir" value="cherry"></label>
+  <input type="submit"/>
+</form>
+<!-- page.html?fruit=cherry&fruit.dir=ltr -->
+
+ +

When the form above is submitted, the input cause both the name / value pair of fruit=cherry and the dirname / direction pair of fruit.dir=ltr to be sent.

+
+
{{htmlattrdef("disabled")}}
+
+

A Boolean attribute which, if present, indicates that the user should not be able to interact with the input. Disabled inputs are typically rendered with a dimmer color or using some other form of indication that the field is not available for use.

+ +

Specifically, disabled inputs do not receive the {{domxref("Element/click_event", "click")}} event, and disabled inputs are not submitted with the form.

+ +
+

Note: Although not required by the specification, Firefox will by default persist the dynamic disabled state of an <input> across page loads. Use the {{htmlattrxref("autocomplete","input")}} attribute to control this feature.

+
+
+
{{htmlattrdef("form")}}
+
+

A string specifying the {{HTMLElement("form")}} element with which the input is associated (that is, its form owner). This string's value, if present, must match the {{htmlattrxref("id")}} of a <form> element in the same document. If this attribute isn't specified, the <input> element is associated with the nearest containing form, if any.

+ +

The form attribute lets you place an input anywhere in the document but have it included with a form elsewhere in the document.

+ +
+

Note: An input can only be associated with one form.

+
+
+
{{htmlattrdef('formaction')}}
+
+

Valid for the image and submit input types only. See the {{HTMLElement("input/submit", "submit")}} input type for more information.

+
+
{{htmlattrdef('formenctype')}}
+
+

Valid for the image and submit input types only. See the {{HTMLElement("input/submit", "submit")}} input type for more information.

+
+
{{htmlattrdef('formmethod')}}
+
+

Valid for the image and submit input types only. See the {{HTMLElement("input/submit", "submit")}} input type for more information.

+
+
{{htmlattrdef('formnovalidate')}}
+
+

Valid for the image and submit input types only. See the {{HTMLElement("input/submit", "submit")}} input type for more information.

+
+
{{htmlattrdef('formtarget')}}
+
+

Valid for the image and submit input types only. See the {{HTMLElement("input/submit", "submit")}} input type for more information.

+
+
{{htmlattrdef("height")}}
+
+

Valid for the image input button only, the height is the height of the image file to display to represent the graphical submit button. See the {{HTMLElement("input/image", "image")}} input type.

+
+
{{htmlattrdef("id")}}
+
+

Global attribute valid for all elements, including all the input types, it defines a unique identifier (ID) which must be unique in the whole document. Its purpose is to identify the element when linking. The value is used as the value of the {{htmlelement('label')}}'s for attribute to link the label with the form control. See {{htmlelement('label')}}.

+
+
{{htmlattrdef("inputmode")}}
+
+

Global value valid for all elements, it provides a hint to browsers as to the type of virtual keyboard configuration to use when editing this element or its contents. Values include none, text, tel, url, email, numeric, decimal, and search.

+
+
{{htmlattrdef("list")}}
+
+

The value given to the list attribute should be. the {{domxref("Element.id", "id")}} of a {{HTMLElement("datalist")}} element located in the same document. The <datalist> provides a list of predefined values to suggest to the user for this input. Any values in the list that are not compatible with the {{htmlattrxref("type", "input")}} are not included in the suggested options. The values provided are suggestions, not requirements: users can select from this predefined list or provide a different value.

+ + + +

{{EmbedLiveSample("datalist",400,275,"","", "nobutton")}}

+ +

It is valid on text, search, url, tel, email, date, month, week, time, datetime-local, number, range, and color.

+ +

Per the specifications, the list attribute is not supported by the hidden, password, checkbox, radio, file, or any of the button types.

+ +

Depending on the browser, the user may see a custom color palette suggested, tic marks along a range, or even a input that opens like a {{HTMLElement("select")}} but allows for non-listed values. Check out the browser compatibility table for the other input types.

+ +

See the {{htmlelement('datalist')}} element.

+
+
{{htmlattrdef("max")}}
+
+

Valid for date, month, week, time, datetime-local, number, and range, it defines the greatest value in the range of permitted values. If the {{htmlattrxref("value", "input")}} entered into the element exceeds this, the element fails constraint validation. If the value of the max attribute isn't a number, then the element has no maximum value.

+ +

There is a special case: if the data type is periodic (such as for dates or times), the value of max may be lower than the value of min, which indicates that the range may wrap around; for example, this allows you to specify a time range from 10 PM to 4 AM.

+
+
{{htmlattrdef("maxlength")}}
+
+

Valid for text, search, url, tel, email, and password, it defines the maximum number of characters (as UTF-16 code units) the user can enter into the field. This must be an integer value 0 or higher. If no maxlength is specified, or an invalid value is specified, the field has no maximum length. This value must also be greater than or equal to the value of minlength.

+ +

The input will fail constraint validation if the length of the text entered into the field is greater than maxlength UTF-16 code units long. By default, browsers prevent users from entering more characters than allowed by the maxlength attribute. See {{anch("Client-side validation")}} for more information.

+
+
{{htmlattrdef("min")}}
+
+

Valid for date, month, week, time, datetime-local, number, and range, it defines the most negative value in the range of permitted values. If the {{htmlattrxref("value", "input")}} entered into the element is less than this this, the element fails constraint validation. If the value of the min attribute isn't a number, then the element has no minimum value.

+ +

This value must be less than or equal to the value of the max attribute. If the min attribute is present but is not specified or is invalid, no min value is applied. If the min attribute is valid and a non-empty value is less than the minimum allowed by the min attribute, constraint validation will prevent form submission. See {{anch("Client-side validation")}} for more information.

+ +

There is a special case: if the data type is periodic (such as for dates or times), the value of max may be lower than the value of min, which indicates that the range may wrap around; for example, this allows you to specify a time range from 10 PM to 4 AM.

+
+
{{htmlattrdef("minlength")}}
+
+

Valid for text, search, url, tel, email, and password, it defines the minimum number of characters (as UTF-16 code units) the user can enter into the entry field. This must be an non-negative integer value smaller than or equal to the value specified by maxlength. If no minlength is specified, or an invalid value is specified, the input has no minimum length.

+ +

The input will fail constraint validation if the length of the text entered into the field is fewer than minlength UTF-16 code units long, preventing form submission. See {{anch("Client-side validation")}} for more information.

+
+
{{htmlattrdef("multiple")}}
+
+

The Boolean multiple attribute, if set, means the user can enter comma separated email addresses in the email widget or can choose more than one file with the file input. See the {{HTMLElement("input/email", "email")}} and {{HTMLElement("input/file", "file")}} input type.

+
+
{{htmlattrdef("name")}}
+
+

A string specifying a name for the input control. This name is submitted along with the control's value when the form data is submitted.

+ +
What's in a name
+ +

Consider the name a required attribute (even though it's not). If an input has no name specified, or name is empty, the input's value is not submitted with the form! (Disabled controls, unchecked radio buttons, unchecked checkboxes, and reset buttons are also not sent.)

+ +

There are two special cases:

+ +
    +
  1. _charset_ : If used as the name of an <input> element of type {{HTMLElement("input/hidden", "hidden")}}, the input's value is automatically set by the {{Glossary("user agent")}} to the character encoding being used to submit the form.
  2. +
  3. isindex: For historical reasons, the name isindex is not allowed.
  4. +
+ +
name and radio buttons
+ +

The name attribute creates a unique behavior for radio buttons.

+ +

Only one radio button in a same-named group of radio buttons can be checked at a time. Selecting any radio button in that group automatically deselects any currently-selected radio button in the same group. The value of that one checked radio button is sent along with the name if the form is submitted,

+ +

When tabbing into a series of same-named group of radio buttons, if one is checked, that one will receive focus. If they aren't grouped together in source order, if one of the group is checked, tabbing into the group starts when the first one in the group is encountered, skipping all those that aren't checked. In other words, if one is checked, tabbing skips the unchecked radio buttons in the group. If none are checked, the radio button group receives focus when the first button in the same name group is reached.

+ +

Once one of the radio buttons in a group has focus, using the arrow keys will navigate through all the radio buttons of the same name, even if the radio buttons are not grouped together in the source order.

+ +
HTMLFormElement.elements
+ +

When an input element is given a name, that name becomes a property of the owning form element's {{domxref("HTMLFormElement.elements")}} property. If you have an input whose name is set to guest and another whose name is hat-size, the following code can be used:

+ +
let form = document.querySelector("form");
+
+let guestName = form.elements.guest;
+let hatSize = form.elements["hat-size"];
+
+ +

When this code has run, guestName will be the {{domxref("HTMLInputElement")}} for the guest field, and hatSize the object for the hat-size field.

+ +
+

Warning: Avoid giving form elements a name that corresponds to a built-in property of the form, since you would then override the predefined property or method with this reference to the corresponding input.

+
+
+
{{htmlattrdef("pattern")}}
+
+
+

The pattern attribute, when specified, is a regular expression that the input's {{htmlattrxref("value")}} must match in order for the value to pass constraint validation. It must be a valid JavaScript regular expression, as used by the {{jsxref("RegExp")}} type, and as documented in our guide on regular expressions; the 'u' flag is specified when compiling the regular expression, so that the pattern is treated as a sequence of Unicode code points, instead of as ASCII. No forward slashes should be specified around the pattern text.

+ +

If the pattern attribute is present but is not specified or is invalid, no regular expression is applied and this attribute is ignored completely. If the pattern attribute is valid and a non-empty value does not match the pattern, constraint validation will prevent form submission.

+ +
+

Tip: If using the pattern attribute, inform the user about the expected format by including explanatory text nearby. You can also include a {{htmlattrxref("title", "input")}} attribute to explain what the requirements are to match the pattern; most browsers will display this title as a tooltip. The visible explanation is required for accessibility. The tooltip is an enhancement.

+
+
+ +

See {{anch("Client-side validation")}} for more information.

+
+
{{htmlattrdef("placeholder")}}
+
+

The placeholder attribute is a string that provides a brief hint to the user as to what kind of information is expected in the field. It should be a word or short phrase that provides a hint as to the expected type of data, rather than an explanation or prompt. The text must not include carriage returns or line feeds. So for example if a field is expected to capture a user's first name, and its label is "First Name", a suitable placeholder might be "e.g. Mustafa".

+ +
+

Note: The placeholder attribute is not as semantically useful as other ways to explain your form, and can cause unexpected technical issues with your content. See {{SectionOnPage("/en-US/docs/Web/HTML/Element/input", "Labels")}} for more information.

+
+
+
{{htmlattrdef("readonly")}}
+
+

A Boolean attribute which, if present, indicates that the user should not be able to edit the value of the input. The readonly attribute is supported text, search, url, tel, email, date, month, week, time, datetime-local, number, and password input types.

+ +

See the HTML attribute: readonly for more information.

+
+
{{htmlattrdef("required")}}
+
+

required is a Boolean attribute which, if present, indicates that the user must specify a value for the input before the owning form can be submitted. The required attribute is supported text, search, url, tel, email, date, month, week, time, datetime-local, number, password, checkbox, radio, and file.

+ +

See {{anch("Client-side validation")}} and the HTML attribute: required for more information.

+
+
{{htmlattrdef("size")}}
+
Valid for email, password, tel, and text input types only. Specifies how much of the input is shown. Basically creates same result as setting CSS width property with a few specialities. The actual unit of the value depends on the input type. For password and text, it is a number of characters (or em units) with a default value of 20, and for others, it is pixels. CSS width takes precedence over size attribute.
+
{{htmlattrdef("src")}}
+
+

Valid for the image input button only, the src is string specifying the URL of the image file to display to represent the graphical submit button. See the {{HTMLElement("input/image", "image")}} input type.

+
+
{{htmlattrdef("step")}}
+
+
+

Valid for the numeric input types, including number, date/time input types, and range, the step attribute is a number that specifies the granularity that the value must adhere to.

+ +

If not explicitly included, step defaults to 1 for number and range, and 1 unit type (second, week, month, day) for the date/time input types. The value can must be a positive number—integer or float—or the special value any, which means no stepping is implied, and any value is allowed (barring other constraints, such as {{anch("min")}} and {{anch("max")}}).

+ +

If any is not explicity set, valid values for the number, date/time input types, and range input types are equal to the basis for stepping - the {{anch("min")}} value and increments of the step value, up to the {{anch("max")}} value, if specified.

+ +

For example, if you have <input type="number" min="10" step="2">, then any even integer, 10 or greater, is valid. If omitted, <input type="number">, any integer is valid, but floats (like 4.2) are not valid, because step defaults to 1. For 4.2 to be valid, step would have had to be set to any, 0.1, 0.2, or any the min value would have had to be a number ending in .2, such as <input type="number" min="-5.2">

+ +
+

Note: When the data entered by the user doesn't adhere to the stepping configuration, the value is considered invalid in contraint validation and will match the :invalid pseudoclass.

+
+
+ +

The step attribute is expressed in seconds. The step scale factor is 1000 (which converts the seconds to milliseconds, as used in the other algorithms). The default step is 60 seconds.

+ +

See {{anch("Client-side validation")}} for more information.

+
+
{{htmlattrdef("tabindex")}}
+
+

Global attribute valid for all elements, including all the input types, an integer attribute indicating if the element can take input focus (is focusable), if it should participate to sequential keyboard navigation. As all input types except for input of type hidden are focusable, this attribute should not be used on form controls, because doing so would require the management of the focus order for all elements within the document with the risk of harming usability and accessibility if done incorrectly.

+
+
{{htmlattrdef('title')}}
+
+

Global attribute valid for all elements, including all input types, containing a text representing advisory information related to the element it belongs to. Such information can typically, but not necessarily, be presented to the user as a tooltip. The title should NOT be used as the primary explanation of the purpose of the form control. Instead, use the {{htmlelement('label')}} element with a for attribute set to the form control's {{htmlattrdef('id')}} attribute. See {{anch("Labels")}} below.

+
+
{{htmlattrdef("type")}}
+
+

A string specifying the type of control to render. For example, to create a checkbox, a value of checkbox is used. If omitted (or an unknown value is specified), the input type text is used, creating a plaintext input field.

+ +

Permitted values are listed in {{anch("<input> types", "<input> types")}} above.

+
+
{{htmlattrdef("value")}}
+
+

The input control's value. When specified in the HTML, this is the initial value, and from then on it can be altered or retrieved at any time using JavaScript to access the respective {{domxref("HTMLInputElement")}} object's value property. The value attribute is always optional, though should be considered mandatory for checkbox, radio, and hidden.

+
+
{{htmlattrdef("width")}}
+
+

Valid for the image input button only, the width is the width of the image file to display to represent the graphical submit button. See the {{HTMLElement("input/image", "image")}} input type.

+
+
+ +

Non-standard attributes

+ +

The following non-standard attributes are also available on some browsers. As a general rule, you should avoid using them unless it can't be helped.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
AttributeDescription
{{anch("autocorrect")}}A string indicating whether or not autocorrect is on or off. Safari only.
{{anch("incremental")}}Whether or not to send repeated {{domxref("HTMLInputElement/search_event", "search")}} events to allow updating live search results while the user is still editing the value of the field. WebKit and Blink only (Safari, Chrome, Opera, etc.).
{{anch("mozactionhint")}}A string indicating the type of action that will be taken when the user presses the Enter or Return key while editing the field; this is used to determine an appropriate label for that key on a virtual keyboard. Firefox for Android only.
{{anch("orient")}}Sets the orientation of the range slider. Firefox only.
{{anch("results")}}The maximum number of items that should be displayed in the drop-down list of previous search queries. Safari only.
{{anch("webkitdirectory")}}A Boolean indicating whether or not to only allow the user to choose a directory (or directories, if {{anch("multiple")}} is also present)
+ +
+
{{htmlattrdef("autocorrect")}} {{non-standard_inline}}
+
{{page("/en-US/docs/Web/HTML/Element/input/text", "autocorrect-include")}}
+
{{htmlattrdef("incremental")}} {{non-standard_inline}}
+
{{page("/en-US/docs/Web/HTML/Element/input/search", "incremental-include")}}
+
{{htmlattrdef("mozactionhint")}} {{non-standard_inline}}
+
{{page("/en-US/docs/Web/HTML/Element/input/text", "mozactionhint-include")}}
+
{{htmlattrdef("orient")}} {{non-standard_inline}}
+
{{page("/en-US/docs/Web/HTML/Element/input/range", "orient-include")}}
+
{{htmlattrdef("results")}} {{non-standard_inline}}
+
{{page("/en-US/docs/Web/HTML/Element/input/search", "results-include")}}
+
{{htmlattrdef("webkitdirectory")}} {{non-standard_inline}}
+
{{page("/en-US/docs/Web/HTML/Element/input/file", "webkitdirectory-include")}}
+
+ +

Methods

+ +

The following methods are provided by the {{domxref("HTMLInputElement")}} interface which represents <input> elements in the DOM. Also available are those methods specified by the parent interfaces, {{domxref("HTMLElement")}}, {{domxref("Element")}}, {{domxref("Node")}}, and {{domxref("EventTarget")}}.

+ +
+
{{domxref("HTMLInputElement.checkValidity", "checkValidity()")}}
+
Immediately runs the validity check on the element, triggering the document to fire the {{domxref("HTMLInputElement.invalid_event", "invalid")}} event at the element if the value isn't valid.
+
{{domxref("HTMLFormElement.reportValidity", "reportValidity()")}}
+
Returns true if the element's value passes validity checks; otherwise, returns false.
+
{{domxref("HTMLInputElement.select", "select()")}}
+
Selects the entire content of the <input> element, if the element's content is selectable. For elements with no selectable text content (such as a visual color picker or calendar date input), this method does nothing.
+
{{domxref("HTMLInputElement.setCustomValidity", "setCustomValidity()")}}
+
Sets a custom message to display if the input element's value isn't valid.
+
{{domxref("HTMLInputElement.setRangeText", "setRangeText()")}}
+
Sets the contents of the specified range of characters in the input element to a given string. A selectMode parameter is available to allow controlling how the existing content is affected.
+
{{domxref("HTMLInputElement.setSelectionRange", "setSelectionRange()")}}
+
Selects the specified range of characters within a textual input element. Does nothing for inputs which aren't presented as text input fields.
+
{{domxref("HTMLInputElement.stepDown", "stepDown()")}}
+
Decrements the value of a numeric input by one, by default, or by the specified number of units.
+
{{domxref("HTMLInputElement.stepUp", "stepUp()")}}
+
Increments the value of a numeric input by one or by the specified number of units.
+
+ +

CSS

+ +

Inputs, being replaced elements, have a few features not applicable to non form elements. There are CSS selectors that can specification target form controls based on their UI features, also known as UI pseudo-classes. The input element can also be targeted by type with attribute selectors. There are some properties that are especially useful as well.

+ +

UI pseudo-classes

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Captions super relevant to the {{htmlelement("input")}} element:
Pseudo-classDescription
{{Cssxref(":enabled")}}Any currently enabled element that can be activated (selected, clicked on, typed into, etc.) or accept focus and also has a disabled state, in which it can't be activated or accept focus.
{{Cssxref(":disabled")}}Any currently disabled element that has an enabled state, meaing it otherwise could be activated (selected, clicked on, typed into, etc.) or accept focus were it not disabled.
{{Cssxref(":read-only")}}Element not editable by the user
{{Cssxref(":read-write")}}Element that is editable by the user.
{{Cssxref(":placeholder-shown")}}Element that is currently displaying placeholder text, including {{HTMLElement("input")}} and {{HTMLElement("textarea")}} elements with the {{anch('htmlattrdefplaceholder', 'placeholder')}} attribute present that has, as of yet, no value.
{{Cssxref(":default")}}Form elements that are the default in a group of related elements. Matches {{HTMLElement("input/checkbox", "checkbox")}} and {{HTMLElement("input/radio", "radio")}} input types that were checked on page load or render.
{{Cssxref(":checked")}}Matches {{HTMLElement("input/checkbox", "checkbox")}} and {{HTMLElement("input/radio", "radio")}} input types that are currently checked (and the ({{HTMLElement("option")}} in a {{HTMLElement("select")}} that is currently selected).
{{Cssxref(":indeterminate")}}{{HTMLElement("input/checkbox", "checkbox")}} elements whose indeterminate property is set to true by JavaScript, {{HTMLElement("input/radio", "radio")}} elements, when all radio buttons with the same name value in the form are unchecked, and {{HTMLElement("progress")}} elements in an indeterminate state
{{Cssxref(":valid")}}Form controls that can have constraint validation applied and are currently valid.
{{Cssxref(":invalid")}}Form controls that have constraint validation applied and are currently not valid. Matches a form control whose value doesn't match the constraints set on it by it's attributes, such as {{anch('htmlattrdefrequired', 'required')}}, {{anch('htmlattrdefpattern', 'pattern')}} , {{anch('htmlattrdefstep', 'step')}} and {{anch('htmlattrdefmax', 'max')}}.
{{Cssxref(":in-range")}}A non-empty input whose current value is within the range limits specified by the {{anch('htmlattrdefmin', 'min')}} and {{anch('htmlattrdefmax', 'max')}} attributes and the {{anch('htmlattrdefstep', 'step')}} .
{{Cssxref(":out-of-range")}}A non-empty input whose current value is NOT within the range limits specified by the {{anch('htmlattrdefmin', 'min')}} and {{anch('htmlattrdefmax', 'max')}} attributes or does not adher to the {{anch('htmlattrdefstep', 'step')}} constraint.
{{Cssxref(":required")}}{{HTMLElement("input")}}, {{HTMLElement("select")}}, or {{HTMLElement("textarea")}} element that has the {{ htmlattrxref("required", "input")}} attribute set on it. Only matches elements that can be required. The attribute included on a non-requirable element will not make for a match.
{{Cssxref(":optional")}}{{HTMLElement("input")}}, {{HTMLElement("select")}}, or {{HTMLElement("textarea")}} element that does NOT have the {{ htmlattrxref("required", "input")}} attribute set on it. Does not match elements that can't be required.
{{Cssxref(":blank")}}{{HTMLElement("input")}} and {{HTMLElement("textarea")}} elements that currently have no value.
{{Cssxref(":user-invalid")}}Similar to :invalid, but is activated on blur. Matches invalid input but only after the user interaction, such as by focusing on the control, leaving the control, or attempting to submit the form containing the invalid control.
+ +

Examples

+ +

We can style a checkbox label based on whether the checkbox is checked or not. In this example, we are styling the {{cssxref('color')}} and {{cssxref('font-weight')}} of the {{htmlelement('label')}} that comes immediately after a checked input. We haven't applied any styles if the input is not checked.

+ +
+ + +
input:checked + label {
+  color: red;
+  font-weight: bold;
+}
+
+ +

{{EmbedLiveSample('checkbox_label', 500, 80)}}

+
+ +

Attribute selectors

+ +

It is possible to target different types of form controls based on their {{anch('htmlattrdeftype', 'type')}} using attribute selectors. CSS attribute selectors match elements based on either just the presence of a attribute or the value of a given attribute.

+ +
/* matches a password input */
+input[type="password"] {}
+
+/* matches a form control whose valid values are limited to a range of values*/
+input[min][max] {}
+
+/* matches a form control with with a pattern attribute */
+ input[pattern] {}
+ +

::placeholder

+ +

By default, the appearance of placeholder text is a translucent or light gray. The {{cssxref('::placeholder')}} pseudo-element is the input's placeholder text. It can be styled with a limited subset of CSS properties.

+ +
::placeholder {
+  color: blue;
+}
+ +

Only the subset of CSS properties that apply to the {{cssxref("::first-line")}} pseudo-element can be used in a rule using ::placeholder in its selector.

+ +

appearance

+ +

The {{cssxref("appearance")}} property enables the displaying of (almost) any element as a platform-native style based on the operating system's theme as well as the removal of any platform-native styling with the none value.

+ +

You could make a <div> look like a radio button with div {appearance: radio;} or a radio look like a checkbox with [type="checkbox] {appearance: checkbox;}, but don't.

+ +

Setting appearance: none removes platform native borders, but not functionality.

+ +

caret-color

+ +

A property specific to text entry-related elements is the CSS {{cssxref("caret-color")}} property, which lets you set the color used to draw the text input caret:

+ +

HTML

+ +
<label for="textInput">Note the red caret:</label>
+<input id="textInput" class="custom" size="32">
+
+ +

CSS

+ +
input.custom {
+  caret-color: red;
+  font: 16px "Helvetica", "Arial", "sans-serif"
+}
+
+ +

Result

+ +

{{EmbedLiveSample('caret-color', 500, 80)}}

+ +

object-position and object-fit

+ +

In certain cases (typically involving non-textual inputs and specialized interfaces), the <input> element is a replaced element. When it is, the position and size of the element's size and positioning within its frame can be adjusted using the CSS {{cssxref("object-position")}} and {{cssxref("object-fit")}} properties

+ +

Styling

+ +

For more information about adding color to elements in HTML, see:

+ + + +

Also see:

+ + + +

Additional features

+ +

Labels

+ +

Labels are needed to associate assistive text with an <input>. The {{HTMLElement("label")}} element provides explanatory information about a form field that is always appropriate (aside from any layout concerns you have). It's never a bad idea to use a <label> to explain what should be entered into an <input> or {{HTMLElement("textarea")}}.

+ +

Associated labels

+ +

The semantic pairing of <input> and <label> elements is useful for assistive technologies such as screen readers. By pairing them using the <label>'s {{htmlattrxref("for", "label")}} attribute, you bond the label to the input in a way that lets screen readers describe inputs to users more precisely.

+ +

It does not suffice to have plain text adjacent to the <input> element,. Rather, usability and accessibility requires the inclusion of either implicit or explicit {{HTMLElement("label")}}:

+ +
<!-- inaccessible -->
+<p>Enter your name: <input id="name" type="text" size="30"></p>
+
+<!-- implicit label -->
+<p><label>Enter your name: <input id="name" type="text" size="30"></label></p>
+
+<!-- explicit label -->
+<p><label for="name">Enter your name: </label><input id="name" type="text" size="30"></p>
+ +

The first example is inaccessible: no relationship exists between the prompt and the <input> element.

+ +

In addition to an accessible name, the label provides a larger 'hit' area for mouse and touch screen users to click on or touch. By pairing a <label> with an <input>, clicking on either one will focus the <input>. If you use plain text to "label" your input, this won't happen. Having the prompt part of the activation area for the input is helpful for people with motor control conditions.

+ +

As web developers, it's important that we never assume that people will know all the things that we know. The diversity of people using the web—and by extension your web site—practically guarantees that some of your site's visitors will have some variation in thought processes and/or circumstances that leads them to interpret your forms very differently from you without clear and properly-presented labels.

+ +

Placeholders are not accessible

+ +

The {{htmlattrxref("placeholder", "input")}} attribute lets you specify text that appears within the <input> element's content area itself when it is empty. The placeholder should never be required in order to understand your forms. It is not a label, and should not be used as a substitute, because it isn't. The placeholder is used to provide a hint as to what an inputted value should look like, not an explanation or prompt.

+ +

Not only is the placeholder not accessible to screen readers, but once the user enters any text into the form control, or if the form control already has a value, the placeholder disappears. Browsers with automatic page translation features may skip over attributes when translating, meaning the placeholder may not get translated.

+ +
+

Don't use the {{htmlattrxref("placeholder", "input")}} attribute if you can avoid it. If you need to label an <input> element, use the {{HTMLElement("label")}} element.

+
+ +

Client-side validation

+ +
+

Warning: Client-side validation is useful, but it does not guarantee that the server will receive valid data. If the data must be in a specific format, always verify it also on the server-side, and return a 400 HTTP response if the format is invalid.

+
+ +

In addition to using CSS to style inputs based on the {{cssxref(":valid")}} or {{cssxref(":invalid")}} UI states based on the current state of each input, as noted in the {{anch('UI pseudo-classes')}} section above, the browser provides for client-side validation on (attempted) form submission. On form submission, if there is a form control that fails constraint validation, supporting browsers will display an error message on the first invalid form control; displaying a default message based on the error type, or a message set by you.

+ +

Some input types and other attributes place limits on what values are valid for a given input. For example, <input type="number" min="2" max="10" step="2"> means only the number 2, 4, 6, 8, or 10 are valid. Several errors could occur, including a rangeUnderflow error if the value is less than 2, rangeOverflow if greater than 10, stepMismatch if the value is a number between 2 and 10, but not an even integer (does not match the requirements of the step attribute), or typeMismatch if the value is not a number.

+ +

For the input types whose domain of possible values is periodic (that is, at the highest possible value, the values wrap back around to the beginning rather than ending), it's possible for the values of the {{htmlattrxref("max")}} and {{htmlattrxref("min")}} properties to be reversed, which indicates that the range of permitted values starts at min, wraps around to the lowest possible value, then continues on until max is reached. This is particularly useful for dates and times, such as when you want to allow the range to be from 8 PM to 8 AM:

+ +
<input type="time" min="20:00" max="08:00" name="overnight">
+ +

Specific attributes and their values can lead to a specific error {{domxref('ValidityState')}}:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Validity object errors depend on the {{htmlelement('input')}} attributes and their values:
AttributeRelevent propertyDescription
{{anch('htmlattrdefmax', 'max')}}{{domxref('validityState.rangeOverflow')}}Occurs when the value is greater than the maximum value as defined by the max attribute
{{anch('htmlattrdefmaxlength', 'maxlength')}}{{domxref('validityState.tooLong')}}Occurs when the number of characters is greater than the number allowed by the maxlength property
{{anch('htmlattrdefmin', 'min')}}{{domxref('validityState.rangeUnderflow')}}Occurs when the value is less than the minimum value as defined by the min attribute
{{anch('htmlattrdefminlength', 'minlength')}}{{domxref('validityState.tooShort')}}Occurs when the number of characters is less than the number required by the minlength property
{{anch('htmlattrdefpattern', 'pattern')}}{{domxref('validityState.patternMismatch')}}Occurs when a pattern attribute is included with a valid regular expression and the value does not match it.
{{anch('htmlattrdefrequired', 'required')}}{{domxref('validityState.valueMissing')}}Occurs when the required attribute is present but the value is null or radio or checkbox is not checked.
{{anch('htmlattrdefstep', 'step')}}{{domxref('validityState.stepMismatch')}}The value doesn't match the step increment. Increment default is 1, so only integers are valid on type="number" is step is not included. step="any" will never throw this error.
{{anch('htmlattrdeftyoe', 'type')}}{{domxref('validityState.typeMismatch')}}Occurs when the value is not of the correct type, for example a email does not contain an @ or a url doesn't contain a protocol.
+ +

If a form control doesn't have the required attribute, no value, or an empty string, is not invalid. Even if the above attributes are present, with the exception of required, and empty string will not lead to an error.

+ +

We can set limits on what values we accept, and supporting browsers will natively validate these form values and alert the user if there is a mistake when the form is submitted.

+ +

In addition to the errors described in the table above, the validityState interface contains the badInput, valid, and customError boolean readonly properties. The validity object includes:

+ + + +

For each of these Boolean properties, a value of true indicates that the specified reason validation may have failed is true, with the exception of the valid property, which is true if the element's value obeys all constraints.

+ +

If there is an error, supporting browsers will both alert the user and prevent the form from being submitted. A word of caution: if a custom error is set to a truthy value (anything other than the empty string or null), the form will be be prevented from being submitted. If there is no custom error message, and none of the other properties return true, valid will be true, and the form can be submitted.

+ +
function validate(input) {
+  let validityState_object = input.validity;
+  if(validityState_object.valueMissing) {
+     input.setCustomValidity('A value is required');
+  } else if (input.rangeUnderflow) {
+    input.setCustomValidity('Your value is too low');
+  } else if (input.rangeOverflow) {
+    input.setCustomValidity('Your value is too high');
+  } else {
+    input.setCustomValidity('');
+  }
+}
+ +

The last line, setting the custom validity message to the error string is vital. If the user makes an error, and the validity is set, it will fail to submit, even if all of the values are valid, until the message is null.

+ +

Example

+ +

If you want to present a custom error message when a field fails to validate, you need to use the Constraint validation features available on <input> (and related) elements. Take the following form:

+ +
<form>
+  <label for="name">Enter username (upper and lowercase letters): </label>
+  <input type="text" name="name" id="name" required pattern="[A-Za-z]+">
+  <button>Submit</button>
+</form>
+ +

The basic HTML form validation features will cause this to produce a default error message if you try to submit the form with either no valid filled in, or a value that does not match the pattern.

+ +

If you wanted to instead display custom error messages, you could use JavaScript like the following:

+ +
const nameInput = document.querySelector('input');
+const form = document.querySelector('form');
+
+nameInput.addEventListener('input', () => {
+  nameInput.setCustomValidity('');
+  nameInput.checkValidity();
+});
+
+nameInput.addEventListener('invalid', () => {
+  if(nameInput.value === '') {
+    nameInput.setCustomValidity('Enter your username!');
+  } else {
+    nameInput.setCustomValidity('Usernames can only contain upper and lowercase letters. Try again!');
+  }
+});
+ +

The example renders like so:

+ +

{{EmbedLiveSample('Client-side_validation')}}

+ +

In brief:

+ + + +
+

Note: Always validate input constraints both client side and server side. Constraint validation doesn't remove the need for validation on the server side. Invalid values can still be sent by older browsers or by bad actors.

+
+ +
+

Note: Firefox supported a proprietary error attribute — x-moz-errormessage — for many versions, which allowed you set custom error messages in a similar way. This has been removed as of version 66 (see {{bug(1513890)}}).

+
+ +

Localization

+ +

The allowed inputs for certain <input> types depend on the locale. In some locales, 1,000.00 is a valid number, while in other locales the valid way to enter this number is 1.000,00.

+ +

Firefox uses the following heuristics to determine the locale to validate the user's input (at least for type="number"):

+ + + +

Technical summary

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Content categoriesFlow content, listed, submittable, resettable, form-associated element, phrasing content. If the {{htmlattrxref("type", "input")}} is not hidden, then labelable element, palpable content.
Permitted contentNone, it is an {{Glossary("empty element")}}.
Tag omissionMust have a start tag and must not have an end tag.
Permitted parentsAny element that accepts phrasing content.
Implicit ARIA role +
    +
  • type=button: button
  • +
  • type=checkbox: checkbox
  • +
  • type=email +
      +
    • with no list attribute: textbox
    • +
    • with list attribute: {{ARIARole("combobox")}}
    • +
    +
  • +
  • type=image: button
  • +
  • type=number: {{ARIARole("spinbutton")}}
  • +
  • type=radio: {{ARIARole("radio")}}
  • +
  • type=range: {{ARIARole("slider")}}
  • +
  • type=reset: button
  • +
  • type=search +
      +
    • with no list attribute: {{ARIARole("searchbox")}}
    • +
    • with list attribute: {{ARIARole("combobox")}}
    • +
    +
  • +
  • type=submit: button
  • +
  • type=tel +
      +
    • with no list attribute: textbox
    • +
    • with list attribute: {{ARIARole("combobox")}}
    • +
    +
  • +
  • type=text +
      +
    • with no list attribute: textbox
    • +
    • with list attribute: {{ARIARole("combobox")}}
    • +
    +
  • +
  • type=url +
      +
    • with no list attribute: textbox
    • +
    • with list attribute: {{ARIARole("combobox")}}
    • +
    +
  • +
  • type=color|date|datetime-local|file|hidden|month|password|time|week: no corresponding role
  • +
+
Permitted ARIA roles +
    +
  • type=button: {{ARIARole("link")}}, {{ARIARole("menuitem")}}, {{ARIARole("menuitemcheckbox")}}, {{ARIARole("menuitemradio")}}, {{ARIARole("option")}}, {{ARIARole("radio")}}, {{ARIARole("switch")}}, {{ARIARole("tab")}}
  • +
  • type=checkbox: {{ARIARole("button")}} when used with aria-pressed, {{ARIARole("menuitemcheckbox")}}, {{ARIARole("option")}}, {{ARIARole("switch")}}
  • +
  • type=image: {{ARIARole("link")}}, {{ARIARole("menuitem")}}, {{ARIARole("menuitemcheckbox")}}, {{ARIARole("menuitemradio")}}, {{ARIARole("radio")}}, {{ARIARole("switch")}}
  • +
  • type=radio: {{ARIARole("menuitemradio")}}
  • +
  • type=text with no list attribute: {{ARIARole("combobox")}}, {{ARIARole("searchbox")}}, {{ARIARole("spinbutton")}}
  • +
  • type=color|date|datetime|datetime-local|email|file|hidden|month|number|password|range|reset|search|submit|tel|url|week or text with list attribute: no role permitted
  • +
+
DOM interface{{domxref("HTMLInputElement")}}
+ +

Specifications

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML WHATWG', 'forms.html#the-input-element', '<input>')}}{{Spec2('HTML WHATWG')}}
{{SpecName('HTML Media Capture', '#the-capture-attribute','capture attribute')}}{{Spec2('HTML Media Capture')}}Adds the capture attribute
{{SpecName('HTML5 W3C', 'sec-forms.html#the-input-element', '<input>')}}{{Spec2('HTML5 W3C')}}
{{SpecName('HTML4.01', 'interact/forms.html#h-17.4', '<form>')}}{{Spec2('HTML4.01')}}
+ +

Accessibility concerns

+ +

Labels

+ +

When including inputs, it is an accessibilty requirement to add labels along side. This is needed so those who use assistive technologies can tell what the input is for. Also, clicking or touching a label gives focus to the label's associated form control. This improves the accessibility and usability for sighted users, increases the area a user can click or touch to activate the form control. this is especially useful (and even needed) for radio buttons and checkboxes, which are tiny. For more information about labels in general see {{anch("Labels")}} .

+ +

The following is an example of how to associate the <label> with an <input> element in the above style. You need to give the <input> an id attribute. The <label> then needs a for attribute whose value is the same as the input's id.

+ +
<label for="peas">Do you like peas?</label>
+<input type="checkbox" name="peas" id="peas">
+
+ +

Size

+ +

Interactive elements such as form input should provide an area large enough that it is easy to activate them. This helps a variety of people, including people with motor control issues and people using non-precise forms of input such as a stylus or fingers. A minimum interactive size of 44×44 CSS pixels is recommended.

+ + + +

Browser compatibility

+ + + +
{{Compat("html.elements.input")}}
+ +

See also

+ + diff --git a/files/ar/web/html/element/input/radio/index.html b/files/ar/web/html/element/input/radio/index.html new file mode 100644 index 0000000000..cad8bda10b --- /dev/null +++ b/files/ar/web/html/element/input/radio/index.html @@ -0,0 +1,356 @@ +--- +title: +slug: Web/HTML/Element/input/radio +translation_of: Web/HTML/Element/input/radio +--- +
{{HTMLRef}}
+ +

{{htmlelement("input")}} عناصر من النوعradio تستخدم بشكل عام فيradio groups—مجموعات من أزرار الاختيار تصف مجموعة من الخيارات ذات الصلة. يمكن تحديد زر اختيار واحد فقط في مجموعة معينة في نفس الوقت. عادةً ما يتم عرض أزرار الاختيار على شكل دوائر صغيرة ، يتم تعبئتها أو إبرازها عند تحديدها.

+ +
{{EmbedInteractiveExample("pages/tabbed/input-radio.html", "tabbed-standard")}}
+ + + +
+

يطلق عليها أزرار الراديو لأنها تبدو وتعمل بطريقة مشابهة للأزرار الانضغاطية في أجهزة الراديو القديمة ، مثل تلك الموضحة أدناه.

+ +

Shows what radio buttons looked like in the olden days.

+
+ +
+

Note:تشبه مربعات الاختيار أزرار الاختيار ، ولكن مع تمييز مهم: أزرار الاختيار مصممة لتحديد قيمة واحدة من مجموعة ، بينما تتيح لك مربعات الاختيار تشغيل وإيقاف القيم الفردية. في حالة وجود عناصر تحكم متعددة ، تسمح أزرار الاختيار بتحديد واحد منها جميعًا ، بينما تسمح مربعات الاختيار بتحديد قيم متعددة.

+
+ + + + + + + + + + + + + + + + + + + + + + + + +
{{anch("Value")}}A {{domxref("DOMString")}} representing the value of the radio button.
Events{{event("change")}} and {{event("input")}}
Supported common attributeschecked and value
IDL attributes{{anch("checked")}} and {{anch("value")}}
Methods{{domxref("HTMLInputElement.select", "select()")}}
+ +

Value

+ +

The value attribute is a {{domxref("DOMString")}} containing the radio button's value. The value is never shown to the user by their {{Glossary("user agent")}}. Instead, it's used to identify which radio button in a group is selected.

+ +

Defining a radio group

+ +

A radio group is defined by giving each of radio buttons in the group the same {{htmlattrxref("name", "input")}}. Once a radio group is established, selecting any radio button in that group automatically deselects any currently-selected radio button in the same group.

+ +

You can have as many radio groups on a page as you like, as long as each has its own unique name.

+ +

For example, if your form needs to ask the user for their preferred contact method, you might create three radio buttons, each with the name property set to contact but one with the {{htmlattrxref("value", "input")}} email, one with the value phone, and one with the value mail. The user never sees the value or the name (unless you expressly add code to display it).

+ +

The resulting HTML looks like this:

+ +
<form>
+  <p>Please select your preferred contact method:</p>
+  <div>
+    <input type="radio" id="contactChoice1"
+     name="contact" value="email">
+    <label for="contactChoice1">Email</label>
+
+    <input type="radio" id="contactChoice2"
+     name="contact" value="phone">
+    <label for="contactChoice2">Phone</label>
+
+    <input type="radio" id="contactChoice3"
+     name="contact" value="mail">
+    <label for="contactChoice3">Mail</label>
+  </div>
+  <div>
+    <button type="submit">Submit</button>
+  </div>
+</form>
+ +

Here you see the three radio buttons, each with the name set to contact and each with a unique value that uniquely identifies that individual radio button within the group. They each also have a unique {{domxref("Element.id", "id")}}, which is used by the {{HTMLElement("label")}} element's {{htmlattrxref("for", "label")}} attribute to associate the labels with the radio buttons.

+ +

You can try out this example here:

+ +

{{EmbedLiveSample('Defining_a_radio_group', 600, 130)}}

+ +

Data representation of a radio group

+ +

When the above form is submitted with a radio button selected, the form's data includes an entry in the form contact=value. For example, if the user clicks on the "Phone" radio button then submits the form, the form's data will include the line contact=phone.

+ +

If you omit the value attribute in the HTML, the submitted form data assigns the value on to the group. In this scenario, if the user clicked on the "Phone" option and submitted the form, the resulting form data would be contact=on, which isn't helpful. So don't forget to set your value attributes!

+ +
+

Note: If no radio button is selected when the form is submitted, the radio group is not included in the submitted form data at all, since there is no value to report.

+
+ +

It's fairly uncommon to actually want to allow the form to be submitted without any of the radio buttons in a group selected, so it is usually wise to have one default to the checked state. See {{anch("Selecting a radio button by default")}} below.

+ +

Let's add a little bit of code to our example so we can examine the data generated by this form. The HTML is revised to add a {{HTMLElement("pre")}} block to output the form data into:

+ +
<form>
+  <p>Please select your preferred contact method:</p>
+  <div>
+    <input type="radio" id="contactChoice1"
+           name="contact" value="email">
+    <label for="contactChoice1">Email</label>
+    <input type="radio" id="contactChoice2"
+           name="contact" value="phone">
+    <label for="contactChoice2">Phone</label>
+    <input type="radio" id="contactChoice3"
+           name="contact" value="mail">
+    <label for="contactChoice3">Mail</label>
+  </div>
+  <div>
+    <button type="submit">Submit</button>
+  </div>
+</form>
+<pre id="log">
+</pre>
+
+ +

Then we add some JavaScript to set up an event listener on the {{domxref("HTMLFormElement/submit_event", "submit")}} event, which is sent when the user clicks the "Submit" button:

+ +
var form = document.querySelector("form");
+var log = document.querySelector("#log");
+
+form.addEventListener("submit", function(event) {
+  var data = new FormData(form);
+  var output = "";
+  for (const entry of data) {
+    output = output + entry[0] + "=" + entry[1] + "\r";
+  };
+  log.innerText = output;
+  event.preventDefault();
+}, false);
+ +

Try this example out and see how there's never more than one result for the contact group.

+ +

{{EmbedLiveSample("Data_representation_of_a_radio_group", 600, 130)}}

+ +

Additional attributes

+ +

In addition to the common attributes shared by all {{HTMLElement("input")}} elements, radio inputs support the following attributes:

+ + + + + + + + + + + + + + + + + + +
AttributeDescription
{{anch("checked")}}A Boolean indicating whether or not this radio button is the currently-selected item in the group
{{anch("value")}}The string to use as the value of the radio when submitting the form, if the radio is currently toggled on
+ +

{{htmlattrdef("checked")}}

+ +

A Boolean attribute which, if present, indicates that this radio button is the currently selected one in the group.

+ +

Unlike other browsers, Firefox by default persists the dynamic checked state of an <input> across page loads. Use the {{htmlattrxref("autocomplete","input")}} attribute to control this feature.

+ +

{{htmlattrdef("value")}}

+ +

The value attribute is one which all {{HTMLElement("input")}}s share; however, it serves a special purpose for inputs of type radio: when a form is submitted, only radio buttons which are currently checked are submitted to the server, and the reported value is the value of the value attribute. If the value is not otherwise specified, it is the string on by default. This is demonstrated in the section {{anch("Value")}} above.

+ +

Using radio inputs

+ +

We already covered the fundamentals of radio buttons above. Let's now look at the other common radio-button-related features and techniques you may need to know about.

+ +

Selecting a radio button by default

+ +

To make a radio button selected by default, you simply include checked attribute, as shown in this revised version of the previous example:

+ +
<form>
+  <p>Please select your preferred contact method:</p>
+  <div>
+    <input type="radio" id="contactChoice1"
+     name="contact" value="email" checked>
+    <label for="contactChoice1">Email</label>
+
+    <input type="radio" id="contactChoice2"
+     name="contact" value="phone">
+    <label for="contactChoice2">Phone</label>
+
+    <input type="radio" id="contactChoice3"
+     name="contact" value="mail">
+    <label for="contactChoice3">Mail</label>
+  </div>
+  <div>
+    <button type="submit">Submit</button>
+  </div>
+</form>
+ +

{{EmbedLiveSample('Selecting_a_radio_button_by_default', 600, 130)}}

+ +

In this case, the first radio button is now selected by default.

+ +
+

Note: If you put the checked attribute on more than one radio button, later instances will override earlier ones; that is, the last checked radio button will be the one that is selected. This is because only one radio button in a group can ever be selected at once, and the user agent automatically deselects others each time a new one is marked as checked.

+
+ +

Providing a bigger hit area for your radio buttons

+ +

In the above examples, you may have noticed that you can select a radio button by clicking on its associated {{htmlelement("label")}} element, as well as on the radio button itself. This is a really useful feature of HTML form labels that makes it easier for users to click the option they want, especially on small-screen devices like smartphones.

+ +

Beyond accessibility, this is another good reason to properly set up <label> elements on your forms.

+ +

Validation

+ +

Radio buttons don't participate in constraint validation; they have no real value to be constrained.

+ +

Styling radio inputs

+ +

The following example shows a slightly more thorough version of the example we've seen throughout the article, with some additional styling, and with better semantics established through use of specialized elements. The HTML looks like this:

+ +
<form>
+  <fieldset>
+    <legend>Please select your preferred contact method:</legend>
+    <div>
+      <input type="radio" id="contactChoice1"
+       name="contact" value="email" checked>
+      <label for="contactChoice1">Email</label>
+
+      <input type="radio" id="contactChoice2"
+       name="contact" value="phone">
+      <label for="contactChoice2">Phone</label>
+
+      <input type="radio" id="contactChoice3"
+       name="contact" value="mail">
+      <label for="contactChoice3">Mail</label>
+    </div>
+    <div>
+      <button type="submit">Submit</button>
+    </div>
+  </fieldset>
+</form>
+ +

There's not much new to note here except for the addition of {{htmlelement("fieldset")}} and {{htmlelement("legend")}} elements, which help to group the functionality nicely and in a semantic way.

+ +

The CSS involved is a bit more significant:

+ +
html {
+  font-family: sans-serif;
+}
+
+div:first-of-type {
+  display: flex;
+  align-items: flex-start;
+  margin-bottom: 5px;
+}
+
+label {
+  margin-right: 15px;
+  line-height: 32px;
+}
+
+input {
+  -webkit-appearance: none;
+  -moz-appearance: none;
+  appearance: none;
+
+  border-radius: 50%;
+  width: 16px;
+  height: 16px;
+
+  border: 2px solid #999;
+  transition: 0.2s all linear;
+  margin-right: 5px;
+
+  position: relative;
+  top: 4px;
+}
+
+input:checked {
+  border: 6px solid black;
+}
+
+button,
+legend {
+  color: white;
+  background-color: black;
+  padding: 5px 10px;
+  border-radius: 0;
+  border: 0;
+  font-size: 14px;
+}
+
+button:hover,
+button:focus {
+  color: #999;
+}
+
+button:active {
+  background-color: white;
+  color: black;
+  outline: 1px solid black;
+}
+ +

Most notable here is the use of the {{cssxref("-moz-appearance")}} property (with prefixes needed to support some browsers). By default, radio buttons (and checkboxes) are styled with the operating system's native styles for those controls. By specifying appearance: none, you can remove the native styling altogether, and create your own styles for them. Here we've used a {{cssxref("border")}} along with {{cssxref("border-radius")}} and a {{cssxref("transition")}} to create a nice animating radio selection. Notice also how the {{cssxref(":checked")}} pseudo-class is used to specify the styles for the radio button's appearance when selected.

+ +
+

Compatibility note: If you wish to use the {{cssxref("appearance")}} property, you should test it very carefully. Although it is supported in most modern browsers, its implementation varies widely. In older browsers, even the keyword none does not have the same effect across different browsers, and some do not support it at all. The differences are smaller in the newest browsers.

+
+ +

{{EmbedLiveSample('Styling_radio_inputs', 600, 120)}}

+ +

Notice that when clicking on a radio button, there's a nice, smooth fade out/in effect as the two buttons change state. In addition, the style and coloring of the legend and submit button are customized to have strong contrast. This might not be a look you'd want in a real web application, but it definitely shows off the possibilities.

+ +

Specifications

+ + + + + + + + + + + + + + + + + + + + + +
SpecificationStatus
{{SpecName('HTML WHATWG', 'forms.html#radio-button-state-(type=radio)', '<input type="radio">')}}{{Spec2('HTML WHATWG')}}
{{SpecName('HTML5 W3C', 'forms.html#radio-button-state-(type=radio)', '<input type="radio">')}}{{Spec2('HTML5 W3C')}}
+ +

Browser compatibility

+ + + +

{{Compat("html.elements.input.input-radio")}}

+ +

See also

+ + -- cgit v1.2.3-54-g00ecf