Skip to content
AnswerWell geo Documentation

Research · 12 min read · Answerwell

HTML Form Controls, Constraint Validation, Submission, and Input Types

How WHATWG HTML forms collect data, validate on the client, submit with GET or POST, and which input types and attributes to use.

This page summarises the WHATWG HTML Living Standard rules for forms: what you can build without JavaScript, how constraint validation works before submit, how data reaches the server, and how to pick input types and shared attributes. Supplementary notes from Google Chrome’s IME-safe enter guidance and directory upload documentation appear where they add practical detail.

A form is a component of a web page that has form controls, such as text, radio buttons, checkboxes, buttons, etc., that the user can use to interact with the page and submit information to the web server, or to manipulate data on the client before submission. No client-side scripting is needed in many cases.

— WHATWG, Forms

form element: markup, categories, and the elements collection

Section titled “form element: markup, categories, and the elements collection”

A form groups form-associated controls (button, fieldset, input, select, textarea, and others). You must not nest a form inside another form. Typical attributes include action, method, enctype, name (unique among document.forms), autocomplete (default on), and accept-charset (when set, only UTF-8 is allowed).

The spec classifies controls for behaviour:

Category Role
Listed Appear in form.elements (or via the form attribute)
Submittable Can contribute entries on submit
Resettable Restored by reset()
Labelable Can be captioned with label

Returns an HTMLFormControlsCollection of the form controls in the form (excluding image buttons for historical reasons).

— WHATWG, The form element

Named lookup on the form (form[name] or elements.namedItem) returns one control, or a RadioNodeList when several share the same name or id.

If there are multiple matching items, then a RadioNodeList object containing all those elements is returned.

— WHATWG, Common DOM interfaces

For a radio group, RadioNodeList.value is the value of the first checked radio in tree order ("on" if value is missing).

Control naming, dirname, and DOM clobbering

Section titled “Control naming, dirname, and DOM clobbering”

The name content attribute defines the field name in the submission body and in form.elements. It must not be empty or the legacy token isindex. A hidden input named _charset_ with no value causes the submission encoding name to be sent automatically.

Avoid name values that match built-in properties of HTMLFormElement (for example method, action, submit):

DOM clobbering is a common cause of security issues. Avoid using the names of built-in form properties with the name content attribute.

— WHATWG, Naming form controls

On controls with automatic directionality, dirname adds a companion entry such as comment and comment.dir with ltr or rtl in the submitted data—useful when mixed-direction text is posted to the server (dirname).

Associate labels with for=id or by wrapping the control; for must reference a labelable element in the same tree.

A control’s value is its internal state. As such, it might not match the user’s current input.

— WHATWG, Form control infrastructure

The dirty value flag tracks whether the user has edited the control; until then, the live value follows the default from the value attribute or (for textarea) initial contents. Sanitisation (for example trimming or normalising email) can make the submitted value differ from raw keystrokes. For select, there is no single “value”; selectedness of options matters.

Boolean attributes on form controls (checked, disabled, required)

Section titled “Boolean attributes on form controls (checked, disabled, required)”

Boolean attributes follow common microsyntax: presence means true, absence means false.

The values “true” and “false” are not allowed on boolean attributes. To represent a false value, the attribute has to be omitted altogether.

— WHATWG, Common microsyntaxes

Writing disabled="false" still disables the control because the attribute is present.

Client-side constraint validation: attributes, events, and server checks

Section titled “Client-side constraint validation: attributes, events, and server checks”

User agents may validate before submit using attributes such as required, pattern, min / max / step, minlength / maxlength, and type-specific rules on input. Invalid controls participate in static validation (checkValidity(), listing invalid events) or interactive validation (same checks plus user-facing reporting via reportValidity()).

The server still has to verify the input is valid (since hostile users can easily bypass the form validation)

— WHATWG, Forms

minlength does not imply required; it applies only once the user has entered something. Hidden inputs are barred from constraint validation. Controls with a datalist ancestor are also barred (datalist). Disabled controls are barred from validation but may still appear in an invalid state in the DOM.

setCustomValidity(message) (and ElementInternals.setValidity for custom elements) sets a custom error; an empty message clears it.

CSS pseudo-classes such as :valid, :invalid, :user-invalid, and :placeholder-shown reflect validation and user interaction (semantics for forms).

form.submit() vs form.requestSubmit(): validation and the submit event

Section titled “form.submit() vs form.requestSubmit(): validation and the submit event”
Method Constraint validation submit event
submit() Bypassed Not fired
requestSubmit([submitter]) Interactive validation runs Fired (cancelable)

form.submit() … Submits the form, bypassing interactive constraint validation and without firing a submit event.

— WHATWG, The form element

form.requestSubmit([ submitter ]) … Unlike submit(), this method includes interactive constraint validation and firing a submit event

— WHATWG, The form element

Use requestSubmit() when scripts or tests must observe submit or when validation must run. A submit button with formnovalidate skips validation for that submission (input submit state).

On a normal submission (not via submit()): interactive validation, then a cancelable SubmitEvent (with submitter), then a formdata event with FormData, then navigation per method and action. reset() fires a cancelable reset event before running reset algorithms on resettable controls.

Implicit submit (Enter in a one-line field)

Section titled “Implicit submit (Enter in a one-line field)”

Pressing Enter in a single-line text-like field typically activates the form’s default submit button (first submit button in tree order). If there is no submit button, implicit submit is allowed only when the form has at most one “blocking” field among text-like types (submission).

There are pages on the web that are only usable if there is a way to implicitly submit forms

— WHATWG, Form control infrastructure

For textarea, Enter inserts a newline unless you add script; for CJK IME composition, Google Chrome’s IME-safe enter guidance recommends blocking submit while event.isComposing is true or keyCode === 229, then calling form.requestSubmit().

Form submission: GET vs POST, enctype, and the entry list

Section titled “Form submission: GET vs POST, enctype, and the entry list”

Data is encoded per enctype and sent to action via method. GET places entries in the query string; POST commonly uses application/x-www-form-urlencoded or multipart/form-data for file fields (form submission).

Skipped from the entry list: controls in a datalist, disabled controls, non-submitting buttons, unchecked checkboxes and radios, and controls with missing or empty name. Checked checkbox/radio values default to "on" without a value attribute. File inputs send selected files; select sends one entry per selected enabled option.

Only the button that initiated submission is included among buttons (button element).

Missing or invalid type defaults to Text. The input element defines one state per keyword (hidden, text, search, tel, url, email, password, date/time types, number, range, color, checkbox, radio, file, submit, image, reset, button).

The input element represents a typed data field, usually with a form control to allow the user to edit the data.

— WHATWG, The input element

Need Prefer Notes
Arbitrary one-line text text or search Search is mainly stylistic (search UI)
Phone number (varied formats) tel No syntax enforcement by default
Absolute URL url Nonempty value must be a valid absolute URL
Email address(es) email Optional multiple for comma-separated list
Secret string password Obscured; no list
Calendar / clock values date, month, week, time, datetime-local step defaults differ (e.g. time default step 60 seconds)
Numeric quantity with spin semantics number Not for credit cards or postal codes
Slider range Native non-devolvable widget (rendering)
Color color Always a CSS color; user cannot clear to empty
On/off or one-of-many checkbox / radio Radio groups share name and form owner
File(s) file multiple, accept; see directory upload below

Unlike the URL and Email types, the Telephone type does not enforce a particular syntax. This is intentional; in practice, telephone number fields tend to be free-form fields, because there are a wide variety of valid phone numbers.

— WHATWG, Telephone state

Use pattern or setCustomValidity() when you need strict phone formats.

The type=number state is not appropriate for input that happens to only consist of numbers but isn’t strictly speaking a number. For example, it would be inappropriate for credit card numbers or US postal codes.

— WHATWG, Number state

For card or ZIP-style strings, prefer type="text" with inputmode / pattern (inputmode in the attribute index).

Constraint validation: While the value of the element is neither the empty string nor a single valid email address, the element is suffering from a type mismatch.

— WHATWG, Email state

Radios group by type=radio, same non-empty name, same form owner, and the same tree. If any radio in the group has required and none are checked, each required member can be missing:

Constraint validation: If an element in the radio button group is required, and all of the input elements in the radio button group have a checkedness that is false, then the element is suffering from being missing.

— WHATWG, Radio state

Putting required on every radio in the group is encouraged so behaviour is predictable (required attribute).

Shared attributes: pattern, placeholder, list

Section titled “Shared attributes: pattern, placeholder, list”
  • pattern: JavaScript RegExp syntax; when used, include a title that describes the pattern, not the field label (pattern).
  • placeholder: short hint when empty only.

The placeholder attribute should not be used as an alternative to a label.

— WHATWG, The input element

  • list: id of a datalist in the same tree; the datalist element itself represents nothing visible—only suggestions.

Which attributes apply to which type is defined in the large applicability table in 4.10.5 The input element; the developer attribute index lists elements and value keywords, with an asterisk when rules are more complex:

An asterisk (*) in a cell indicates that the actual rules are more complicated than indicated in the table above.

— WHATWG, Edition for Web Developers — Index

disabled vs readonly: submit and interaction

Section titled “disabled vs readonly: submit and interaction”

A control is disabled if it has the disabled attribute or sits in a disabled fieldset (except inside the first legend). Disabled controls do not receive clicks for activation, are barred from constraint validation, and are omitted from submission (disabled).

readonly (on text-like input and textarea) keeps the control functional but not mutable; read-only fields are still submitted unless disabled.

The difference between disabled and readonly is that read-only controls can still function, whereas disabled controls generally do not function as controls until they are enabled.

— WHATWG, The readonly attribute

Use disabled when the value must not participate; use readonly when the value should post but not be edited.

For modal overlays, prefer disabled on fields rather than inert on individual controls:

Authors should not specify the inert attribute on individual form controls. In these instances, the disabled attribute is probably more appropriate.

— WHATWG, The inert attribute

button vs input submit controls and commands

Section titled “button vs input submit controls and commands”

The button element is labelled by its contents. type is submit, reset, or button; missing/invalid type is Auto, which acts as submit when it is not a command/commandfor button and is not the first child of select. Submit buttons may use formaction, formmethod, formenctype, formnovalidate, and formtarget. Modern buttons also support command / commandfor (popover, dialog, or custom -- commands) per interactive elements.

textarea holds multiline text with rows / cols, soft or hard wrap, and parallel validation attributes; newline length for maxlength counts normalized line breaks as one character.

File inputs use accept for MIME hints (index). To let users pick a directory, HTML uses the webkitdirectory attribute:

For opening folders (or directories), you can set the <input webkitdirectory> attribute. Apart from that, everything else works the same as above. Despite its vendor-prefixed name, webkitdirectory is not only usable in Chromium and WebKit browsers, but also in the legacy EdgeHTML-based Edge as well as in Firefox.

— Google Chrome Developers, Reading and writing files and directories

Text direction (dir) and translation (translate)

Section titled “Text direction (dir) and translation (translate)”

For bidirectional UI, the spec prefers the dir attribute over CSS alone:

Authors are strongly encouraged to use the dir attribute to indicate text direction rather than using CSS

— WHATWG, dir attribute

On text controls, dir="auto" uses a crude first-strong-character heuristic; pair with dirname when the server must know direction at submit time. The translate attribute (yes / no) controls whether localisation pipelines should translate content and listed attributes (translate).

Mobile keyboards: inputmode and enterkeyhint

Section titled “Mobile keyboards: inputmode and enterkeyhint”

inputmode (none, text, tel, url, email, numeric, decimal, search) hints which virtual keyboard to show; unspecified behaviour depends on type and pattern (index). enterkeyhint labels the Enter key (search, send, next, etc.). These are hints only—not a substitute for validation.

Scripts running under Content Security Policy may use the nonce attribute; the value is stored internally and the content attribute is cleared to limit exfiltration (URLs and fetching).

  • Using required="false" or disabled="false" on boolean attributes.
  • Calling form.submit() when you need validation or a submit handler—use requestSubmit().
  • Client-only validation with no server-side checks.
  • name values that clobber form.method or similar.
  • type="number" for credit cards, phone numbers, or postal codes.
  • placeholder without a real <label>.
  • pattern without a title that explains the pattern.
  • required on a single radio while siblings are optional—group semantics still apply.
  • readonly when you meant to exclude a field from POST (disabled skips submission).
  • Enter-to-submit in a textarea during IME composition without guarding composition state.
  • Assuming webkitdirectory is Chromium-only (Firefox and legacy Edge are documented as supported).
  1. Open the form in browser DevTools, select a control, and run checkValidity() and reportValidity(); confirm :valid / :invalid match your expectations per constraint validation.
  2. Submit with the UI and compare the request body or query string to the entry list rules (disabled and unnamed fields should be absent).
  3. Repeat submission after calling requestSubmit() vs submit() and confirm whether the submit event and validation ran (HTMLFormElement methods).
  4. For attribute support on a given input type, cross-check 4.10.5 and the developer index; follow links when a cell is marked with *.
  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact wording you are asking about, and we will fix or clarify it.