So sánh 2 form input html năm 2024

The

Show

    7 HTML 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 user agent. The

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

    How an

    7 works varies considerably depending on the value of its attribute, hence the different types are covered in their own separate reference pages. If this attribute is not specified, the default type adopted is

    1.

    The available types are as follows:

    Type Description Basic Examples button A push button with no default behavior displaying the value of the attribute, empty by default.

    checkbox A check box allowing single values to be selected/deselected.

    color A control for specifying a color; opening a color picker when active in supporting browsers.

    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.

    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.

    email A field for editing an email address. Looks like a

    1 input, but has validation parameters and relevant keyboard in supporting browsers and devices with dynamic keyboards.

    file A control that lets the user select a file. Use the attribute to define the types of files that the control can select.

    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!

    image A graphical

    5 button. Displays an image defined by the

    6 attribute. The attribute displays if the image is missing.

    month A control for entering a month and year, with no time zone.

    number A control for entering a number. Displays a spinner and adds default validation. Displays a numeric keypad in some devices with dynamic keypads.

    0

    password A single-line text field whose value is obscured. Will alert user if site is not secure.

    1

    radio A radio button, allowing a single value to be selected out of multiple choices with the same value.

    2

    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 and to define the range of acceptable values.

    3

    reset A button that resets the contents of the form to default values. Not recommended.

    4

    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.

    5

    submit A button that submits the form.

    6

    tel A control for entering a telephone number. Displays a telephone keypad in some devices with dynamic keypads.

    7

    text The default value. A single-line text field. Line-breaks are automatically removed from the input value.

    8

    time A control for entering a time value with no time zone.

    9

    url A field for entering a URL. Looks like a

    1 input, but has validation parameters and relevant keyboard in supporting browsers and devices with dynamic keyboards.

    0

    week A control for entering a date consisting of a week-year number and a week number with no time zone.

    1

    Obsolete values

    3 Deprecated A control for entering a date and time (hour, minute, second, and fraction of a second) based on UTC time zone.

    2

    The

    7 element is so powerful because of its attributes; the attribute, described with examples above, being the most important. Since every

    7 element, regardless of type, is based on the

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

    Attributes for the

    7 element include the global HTML attributes and additionally:

    Attribute Type or Types Description

    0 Hint for expected file type in file upload controls

    2 alt attribute for the image type. Required for accessibility all except

    4,

    5, and

    6 Controls automatic capitalization in inputted text. all except

    8,

    9, and buttons Hint for form autofill feature

    0 Media capture input method in file upload controls

    8,

    9 Whether the command or control is checked

    6,

    1,

    8,

    4,

    0,

    5 Name of form field to use for sending the element's directionality in form submission all Whether the form control is disabled all Associates the control with a form element

    2,

    5 URL to use for form submission

    2,

    5 Form data set encoding type to use for form submission

    2,

    5 HTTP method to use for form submission

    2,

    5 Bypass form control validation for form submission

    2,

    5 Browsing context for form submission

    2 Same as height attribute for

    01; vertical dimension all except

    6,

    6,

    8,

    9, and buttons Value of the id attribute of the

    07 of autocomplete options

    09,

    10,

    11,

    12,

    13,

    14,

    15 Maximum value

    1,

    8,

    4,

    0,

    5,

    6 Maximum length (number of characters) of

    2

    09,

    10,

    11,

    12,

    13,

    14,

    15 Minimum value

    1,

    8,

    4,

    0,

    5,

    6 Minimum length (number of characters) of

    2

    5,

    0 Boolean. Whether to allow multiple values all Name of the form control. Submitted with the form as part of a name/value pair

    1,

    8,

    4,

    0,

    5,

    6 Pattern the

    2 must match to be valid

    1,

    8,

    4,

    0,

    5,

    6,

    14 Text that appears in the form control when it has no value set

    61 Designates an

    62 as a control for a popover element

    61 Specifies the action that a popover control should perform all except

    6,

    15,

    68,

    8,

    9, and buttons Boolean. The value is not editable all except

    6,

    15,

    68, and buttons Boolean. A value is required or must be checked for the form to be submittable

    1,

    8,

    4,

    0,

    5,

    6 Size of the control

    2 Same as

    6 attribute for

    01; address of image resource

    09,

    10,

    11,

    12,

    13,

    14,

    15 Incremental values that are valid all Type of form control all except

    2 The initial value of the control

    2 Same as

    97 attribute for

    01

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

    Valid for the

    0 input type only, the

    4 attribute defines which file types are selectable in a

    0 upload control. See the file input type.

    Valid for the

    2 button only, the

    7 attribute provides alternative text for the image, displaying the value of the attribute if the image is missing or otherwise fails to load. See the image input type.

    Controls whether inputted text is automatically capitalized and, if so, in what manner. See the

    3 global attribute page for more information.

    7

    (Not a Boolean attribute!) The

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

    5 addresses in an email input field. See for permitted values.

    The

    7 attribute is valid on

    6,

    1,

    8,

    4,

    0,

    5,

    09,

    10,

    11,

    12,

    13,

    14,

    15,

    68, and

    6. This attribute has no effect on input types that do not return numeric or text data, being valid for all input types except

    8,

    9,

    0, or any of the button types.

    See the

    7 attribute for additional information, including information on password security and how

    7 is slightly different for

    6 than for other input types.

    A Boolean attribute which, if present, indicates that the input should automatically have focus when the page has finished loading (or when the

    38 containing the element has been displayed).

    Note: An element with the

    37 attribute may gain focus before the

    40 event is fired.

    No more than one element in the document may have the

    37 attribute. If put on more than one element, the first one with the attribute receives focus.

    The

    37 attribute cannot be used on inputs of type

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

    37 is assigned, screen-readers "teleport" their user to the form control without warning them beforehand.

    Use careful consideration for accessibility when applying the

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

    Introduced in the HTML Media Capture specification and valid for the

    0 input type only, the

    0 attribute defines which media—microphone, video, or camera—should be used to capture a new file for upload with

    0 upload control in supporting scenarios. See the file input type.

    Valid for both

    9 and

    8 types,

    2 is a Boolean attribute. If present on a

    9 type, it indicates that the radio button is the currently selected one in the group of same-named radio buttons. If present on a

    8 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

    7's

    2 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

    2. If they are, the name and the value(s) of the checked controls are submitted.

    For example, if a checkbox whose

    9 is

    60 has a

    2 of

    62, and the checkbox is checked, the form data submitted will include

    63. If the checkbox isn't active, it isn't listed in the form data at all. The default

    2 for checkboxes and radio buttons is

    65.

    Valid for

    6,

    1,

    8,

    4,

    0, and

    5 input types, the

    5 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 and , and the second being the value of the

    5 attribute as the name, with a value of

    77 or

    78 as set by the browser.

    3

    When the form above is submitted, the input cause both the

    9 /

    2 pair of

    63 and the

    5 / direction pair of

    83 to be sent. For more information, see the

    5 attribute.

    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

    86 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

    7 across page loads. Use the attribute to control this feature.

    A string specifying the

    90 element with which the input is associated (that is, its form owner). This string's value, if present, must match the of a

    90 element in the same document. If this attribute isn't specified, the

    7 element is associated with the nearest containing form, if any.

    The

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

    Valid for the

    2 and

    5 input types only. See the submit input type for more information.

    Valid for the

    2 and

    5 input types only. See the submit input type for more information.

    Valid for the

    2 and

    5 input types only. See the submit input type for more information.

    Valid for the

    2 and

    5 input types only. See the submit input type for more information.

    Valid for the

    2 and

    5 input types only. See the submit input type for more information.

    Valid for the

    2 input button only, the

    9 is the height of the image file to display to represent the graphical submit button. See the image input type.

    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

    14's

    15 attribute to link the label with the form control. See

    14.

    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

    18,

    1,

    0,

    4,

    5,

    23,

    24, and

    8.

    The value given to the

    02 attribute should be the

    91 of a

    07 element located in the same document. The

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

    It is valid on

    1,

    8,

    4,

    0,

    5,

    09,

    10,

    11,

    12,

    13,

    14,

    15, and

    68.

    Per the specifications, the

    02 attribute is not supported by the

    6,

    6,

    8,

    9,

    0, 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 an input that opens like a

    51 but allows for non-listed values. Check out the for the other input types.

    See the

    07 element.

    Valid for

    09,

    10,

    11,

    12,

    13,

    14, and

    15, it defines the greatest value in the range of permitted values. If the entered into the element exceeds this, the element fails constraint validation. If the value of the

    1 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

    1 may be lower than the value of

    0, which indicates that the range may wrap around; for example, this allows you to specify a time range from 10 PM to 4 AM.

    Valid for

    1,

    8,

    4,

    0,

    5, and

    6, it defines the maximum string length (measured in UTF-16 code units) that the user can enter into the field. This must be an integer value of 0 or higher. If no

    16 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

    32.

    The input will fail constraint validation if the length of the text entered into the field is greater than

    16 UTF-16 code units long. By default, browsers prevent users from entering more characters than allowed by the

    16 attribute. See for more information.

    Valid for

    09,

    10,

    11,

    12,

    13,

    14, and

    15, it defines the most negative value in the range of permitted values. If the entered into the element is less than this, the element fails constraint validation. If the value of the

    0 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

    1 attribute. If the

    0 attribute is present but is not specified or is invalid, no

    0 value is applied. If the

    0 attribute is valid and a non-empty value is less than the minimum allowed by the

    0 attribute, constraint validation will prevent form submission. See for more information.

    There is a special case: if the data type is periodic (such as for dates or times), the value of

    1 may be lower than the value of

    0, which indicates that the range may wrap around; for example, this allows you to specify a time range from 10 PM to 4 AM.

    Valid for

    1,

    8,

    4,

    0,

    5, and

    6, it defines the minimum string length (measured in UTF-16 code units) that the user can enter into the entry field. This must be a non-negative integer value smaller than or equal to the value specified by

    16. If no

    32 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

    32 UTF-16 code units long, preventing form submission. See for more information.

    The Boolean

    40 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

    0 input. See the email and file input type.

    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.

    Consider the

    9 a required attribute (even though it's not). If an input has no

    9 specified, or

    9 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. 10 : If used as the name of an

      7 element of type hidden, the input's

      2 is automatically set by the user agent to the character encoding being used to submit the form.
    2. 13: For historical reasons, the name is not allowed.

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

    When an input element is given a

    9, that name becomes a property of the owning form element's

    17 property. If you have an input whose

    9 is set to

    19 and another whose

    9 is

    21, the following code can be used:

    4

    When this code has run,

    22 will be the

    7 for the

    19 field, and

    25 the object for the

    21 field.

    Warning: Avoid giving form elements a

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

    Valid for

    1,

    8,

    4,

    0,

    5, and

    6, the

    44 attribute defines a regular expression that the input's must match in order for the value to pass constraint validation. It must be a valid JavaScript regular expression, as used by the

    37 type, and as documented in our guide on regular expressions; the

    38 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

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

    Note: If using the

    44 attribute, inform the user about the expected format by including explanatory text nearby. You can also include a 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 for more information.

    Valid for

    1,

    8,

    4,

    0,

    5,

    6, and

    14, the

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

    52 attribute is not as semantically useful as other ways to explain your form, and can cause unexpected technical issues with your content. See for more information.

    Turns an

    62 element into a popover control button; takes the ID of the popover element to control as its value. See the Popover API landing page for more details.

    Specifies the action to be performed on a popover element being controlled by a control

    62. Possible values are:

    The button will hide a shown popover. If you try to hide an already hidden popover, no action will be taken.

    The button will show a hidden popover. If you try to show an already showing popover, no action will be taken.

    The button will toggle a popover between showing and hidden. If the popover is hidden, it will be shown; if the popover is showing, it will be hidden. If

    63 is omitted,

    58 is the default action that will be performed by the control button.

    A Boolean attribute which, if present, indicates that the user should not be able to edit the value of the input. The

    65 attribute is supported by the

    1,

    8,

    4,

    0,

    5,

    09,

    10,

    11,

    12,

    13,

    14, and

    6 input types.

    See the HTML attribute:

    65 for more information.

    71 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

    71 attribute is supported by

    1,

    8,

    4,

    0,

    5,

    09,

    10,

    11,

    12,

    13,

    14,

    6,

    8,

    9, and

    0 inputs.

    See and the HTML attribute:

    71 for more information.

    Valid for

    5,

    6,

    0,

    4, and

    1, the

    75 attribute specifies how much of the input is shown. Basically creates same result as setting CSS

    97 property with a few specialities. The actual unit of the value depends on the input type. For

    6 and

    1, it is a number of characters (or

    05 units) with a default value of

    06, and for others, it is pixels (or

    07 units). CSS

    97 takes precedence over the

    75 attribute.

    Valid for the

    2 input button only, the

    6 is string specifying the URL of the image file to display to represent the graphical submit button. See the image input type.

    Valid for

    09,

    10,

    11,

    12,

    13,

    14, and

    15, the

    86 attribute is a number that specifies the granularity that the value must adhere to.

    If not explicitly included:

    • 86 defaults to 1 for

      14 and

      15.
    • Each date/time input type has a default

      86 value appropriate for the type; see the individual input pages: , , , , and .

    The value must be a positive number—integer or float—or the special value

    31, which means no stepping is implied, and any value is allowed (barring other constraints, such as and ).

    If

    31 is not explicitly set, valid values for the

    14, date/time input types, and

    15 input types are equal to the basis for stepping — the value and increments of the step value, up to the value, if specified.

    For example, if you have

    39, then any even integer,

    40 or greater, is valid. If omitted,

    41, any integer is valid, but floats (like

    1. are not valid, because

    86 defaults to

    44. For

    42 to be valid,

    86 would have had to be set to

    31, 0.1, 0.2, or any the

    0 value would have had to be a number ending in

    49, such as

    50

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

    51 pseudoclass.

    See for more information.

    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.

    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

    14 element with a

    15 attribute set to the form control's attribute. See below.

    A string specifying the type of control to render. For example, to create a checkbox, a value of

    8 is used. If omitted (or an unknown value is specified), the input type

    1 is used, creating a plaintext input field.

    Permitted values are listed in above.

    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

    7 object's

    2 property. The

    2 attribute is always optional, though should be considered mandatory for

    8,

    9, and

    6.

    Valid for the

    2 input button only, the

    97 is the width of the image file to display to represent the graphical submit button. See the image input type.

    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.

    Attribute Description A string indicating whether autocorrect is

    65 or

    72. Safari only. Whether or not to send repeated

    8 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.).

    75 Deprecated

    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. Since this attribute is deprecated, use

    76 instead.

    Sets the orientation of the range slider. Firefox only. The maximum number of items that should be displayed in the drop-down list of previous search queries. Safari only. A Boolean indicating whether to only allow the user to choose a directory (or directories, if is also present)Non-standard

    (Safari only). A string which indicates whether to activate automatic correction while the user is editing this field. Permitted values are:

    Enable automatic correction of typos, as well as processing of text substitutions if any are configured.

    Disable automatic correction and text substitutions.

    Non-standard

    The Boolean attribute

    73 is a WebKit and Blink extension (so supported by Safari, Opera, Chrome, etc.) which, if present, tells the user agent to process the input as a live search. As the user edits the value of the field, the user agent sends

    8 events to the

    7 object representing the search box. This allows your code to update the search results in real time as the user edits the search.

    If

    73 is not specified, the

    8 event is only sent when the user explicitly initiates a search (such as by pressing the Enter or Return key while editing the field).

    The

    8 event is rate-limited so that it is not sent more frequently than an implementation-defined interval.

    Non-standard

    Similar to the -moz-orient non-standard CSS property impacting the

    92 and

    93 elements, the

    77 attribute defines the orientation of the range slider. Values include

    95, meaning the range is rendered horizontally, and

    96, where the range is rendered vertically.

    Non-standard

    The

    78 attribute—supported only by Safari—is a numeric value that lets you override the maximum number of entries to be displayed in the

    7 element's natively-provided drop-down menu of previous search queries.

    The value must be a non-negative decimal number. If not provided, or an invalid value is given, the browser's default maximum number of entries is used.

    Non-standard

    The Boolean

    79 attribute, if present, indicates that only directories should be available to be selected by the user in the file picker interface. See

    02 for additional details and examples.

    Though originally implemented only for WebKit-based browsers,

    79 is also usable in Microsoft Edge as well as Firefox 50 and later. However, even though it has relatively broad support, it is still not standard and should not be used unless you have no alternative.

    The following methods are provided by the

    7 interface which represents

    7 elements in the DOM. Also available are those methods specified by the parent interfaces,

    06,

    07,

    08, and

    09.

    10

    Returns

    11 if the element's value passes validity checks; otherwise, returns

    12 and fires an

    13 event at the element.

    14

    Returns

    11 if the element's value passes validity checks; otherwise, returns

    12, fires an

    13 event at the element, and (if the event isn't canceled) reports the problem to the user.

    18

    Selects the entire content of the

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

    20

    Sets a custom message to display if the input element's value isn't valid.

    21

    Sets the contents of the specified range of characters in the input element to a given string. A

    22 parameter is available to allow controlling how the existing content is affected.

    23

    Selects the specified range of characters within a textual input element. Does nothing for inputs which aren't presented as text input fields.

    24

    Displays the browser picker for the input element that would normally be displayed when the element is selected, but triggered from a button press or other user interaction.

    25

    Decrements the value of a numeric input by one, by default, or by the specified number of units.

    26

    Increments the value of a numeric input by one or by the specified number of units.

    Inputs, being replaced elements, have a few features not applicable to non form elements. There are CSS selectors that can specifically 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.

    Captions super relevant to the

    7 element: Pseudo-class Description

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

    29 Any currently disabled element that has an enabled state, meaning it otherwise could be activated (selected, clicked on, typed into, etc.) or accept focus were it not disabled.

    30 Element not editable by the user

    31 Element that is editable by the user.

    32 Element that is currently displaying , including

    7 and

    35 elements with the attribute present that has, as yet, no value.

    37 Form elements that are the default in a group of related elements. Matches checkbox and radio input types that were checked on page load or render.

    38 Matches checkbox and radio input types that are currently checked (and the (

    39 in a

    51 that is currently selected).

    41 checkbox elements whose indeterminate property is set to true by JavaScript, radio elements, when all radio buttons with the same name value in the form are unchecked, and

    92 elements in an indeterminate state

    43 Form controls that can have constraint validation applied and are currently valid.

    51 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 its attributes, such as , , and .

    49 A non-empty input whose current value is within the range limits specified by the and attributes and the .

    53 A non-empty input whose current value is NOT within the range limits specified by the and attributes or does not adhere to the constraint.

    57

    7,

    51, or

    35 element that has the 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.

    62

    7,

    51, or

    35 element that does NOT have the attribute set on it. Does not match elements that can't be required.

    67

    7 and

    35 elements that currently have no value.

    70 Similar to

    51, 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.

    Pseudo-classes example

    We can style a checkbox label based on whether the checkbox is checked or not. In this example, we are styling the

    68 and

    73 of the

    14 that comes immediately after a checked input. We haven't applied any styles if the

    75 is not checked.

    5

    6

    It is possible to target different types of form controls based on their using attribute selectors. CSS attribute selectors match elements based on either just the presence of an attribute or the value of a given attribute.

    7

    By default, the appearance of placeholder text is a translucent or light gray. The

    77 pseudo-element is the input's . It can be styled with a limited subset of CSS properties.

    8

    Only the subset of CSS properties that apply to the

    79 pseudo-element can be used in a rule using

    77 in its selector.

    The

    81 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

    18 value.

    You could make a

    83 look like a radio button with

    84 or a radio look like a checkbox with

    85, but don't.

    Setting

    86 removes platform native borders, but not functionality.

    A property specific to text entry-related elements is the CSS

    87 property, which lets you set the color used to draw the text input caret:

    HTML

    9

    CSS

    0

    Result

    In certain cases (typically involving non-textual inputs and specialized interfaces), the

    7 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

    89 and

    90 properties

    Labels are needed to associate assistive text with an

    7. The

    14 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

    14 to explain what should be entered into an

    7 or

    35.

    Associated labels

    The semantic pairing of

    7 and

    14 elements is useful for assistive technologies such as screen readers. By pairing them using the

    14's 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

    7 element. Rather, usability and accessibility requires the inclusion of either implicit or explicit

    14:

    1

    The first example is inaccessible: no relationship exists between the prompt and the

    7 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

    14 with an

    7, clicking on either one will focus the

    7. 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 website—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 attribute lets you specify text that appears within the

    7 element's content area itself when it is empty. The placeholder should never be required 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

    52 may not get translated.

    Note: Don't use the attribute if you can avoid it. If you need to label an

    7 element, use the

    14 element.

    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

    12 HTTP response if the format is invalid.

    In addition to using CSS to style inputs based on the

    43 or

    51 UI states based on the current state of each input, as noted in the 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,

    15 means only the number 2, 4, 6, 8, or 10 are valid. Several errors could occur, including a

    16 error if the value is less than 2,

    17 if greater than 10,

    18 if the value is a number between 2 and 10, but not an even integer (does not match the requirements of the

    86 attribute), or

    20 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 and properties to be reversed, which indicates that the range of permitted values starts at

    0, wraps around to the lowest possible value, then continues on until

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

    2

    Specific attributes and their values can lead to a specific error

    25:

    Validity object errors depend on the

    7 attributes and their values: Attribute Relevant property Description

    28 Occurs when the value is greater than the maximum value as defined by the

    1 attribute

    31 Occurs when the number of characters is greater than the number allowed by the

    16 property

    34 Occurs when the value is less than the minimum value as defined by the

    0 attribute

    37 Occurs when the number of characters is less than the number required by the

    32 property

    40 Occurs when a pattern attribute is included with a valid regular expression and the

    2 does not match it.

    43 Occurs when the

    71 attribute is present but the value is

    45 or radio or checkbox is not checked.

    47 The value doesn't match the step increment. Increment default is

    44, so only integers are valid on

    49 is step is not included.

    50 will never throw this error.

    52 Occurs when the value is not of the correct type, for example an email does not contain an

    53 or a url doesn't contain a protocol.

    If a form control doesn't have the

    71 attribute, no value, or an empty string, is not invalid. Even if the above attributes are present, with the exception of

    71, an 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

    56 interface contains the

    57,

    58, and

    59 boolean readonly properties. The validity object includes:

    • 43
    • 52
    • 40
    • 31
    • 37
    • 34
    • 28
    • 47
    • 68
    • 69
    • 70

    For each of these Boolean properties, a value of

    11 indicates that the specified reason validation may have failed is true, with the exception of the

    58 property, which is

    11 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

    45), the form will be prevented from being submitted. If there is no custom error message, and none of the other properties return true,

    58 will be true, and the form can be submitted.

    3

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

    45.

    Custom validation error example

    If you want to present a custom error message when a field fails to validate, you need to use the available on

    7 (and related) elements. Take the following form:

    4

    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

    44.

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

    5

    The example renders like so:

    In brief:

    • We check the valid state of the input element every time its value is changed by running the

      10 method via the

      75 event handler.
    • If the value is invalid, an

      13 event is raised, and the

      13 event handler function is run. Inside this function we work out whether the value is invalid because it is empty, or because it doesn't match the pattern, using an

      83 block, and set a custom validity error message.
    • As a result, if the input value is invalid when the submit button is pressed, one of the custom error messages will be shown.
    • If it is valid, it will submit as you'd expect. For this to happen, the custom validity has to be cancelled, by invoking

      20 with an empty string value. We therefore do this every time the

      75 event is raised. If you don't do this, and a custom validity was previously set, the input will register as invalid, even if it currently contains a valid value on submission.

    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 —

    86 — for many versions, which allowed you set custom error messages in a similar way. This has been removed as of version 66 (see Firefox bug 1513890).

    The allowed inputs for certain

    7 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

    88):

    • Try the language specified by a

      89/

      90 attribute on the element or any of its parents.
    • Try the language specified by any

      91 HTTP header. Or,
    • If none specified, use the browser's locale.

    Content categories , listed, submittable, resettable, form-associated element, . If the is not

    6, then labelable element, palpable content. Permitted content None; it is a void element. Tag omission Must have a start tag and must not have an end tag. Permitted parents Any element that accepts . Implicit ARIA role

    • 94:

      95
    • 96:

      97
    • 98

      • with no

        02 attribute:

        00
      • with

        02 attribute:

        02
    • 03:

      95
    • 05:

      06
    • 07:

      9
    • 09:

      10
    • 11:

      95
    • 13

      • with no

        02 attribute:

        15
      • with

        02 attribute:

        02
    • 18:

      95
    • 20

      • with no

        02 attribute:

        00
      • with

        02 attribute:

        02
    • 25

      • with no

        02 attribute:

        00
      • with

        02 attribute:

        02
    • 30

      • with no

        02 attribute:

        00
      • with

        02 attribute:

        02
    • 35: Permitted ARIA roles
    • 94:

      8,

      02,

      39,

      40,

      41,

      42,

      43,

      9,

      45,

      46
    • 96:

      61 when used with

      49,

      41,

      43,

      45
    • 03:

      39,

      40,

      41,

      42,

      9,

      45
    • 07:

      42
    • 25 with no

      02 attribute:

      02,

      15,

      06
    • 67

      68 or

      1 with

      02 attribute: no

      71 permitted DOM interface

    7

    When including inputs, it is an accessibility requirement to add labels alongside. 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 .

    The following is an example of how to associate the

    14 with an

    7 element in the above style. You need to give the

    7 an

    91 attribute. The

    14 then needs a

    15 attribute whose value is the same as the input's

    91.

    6

    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 is recommended.