HTML Form Attributes

HTML Form Attributes

Master every attribute that controls form behavior, validation, security, and user experience. From basic action and method to advanced autocomplete tokens.

What are the most important HTML form attributes?

On <form>: action (URL to submit to), method (get or post), enctype (set to multipart/form-data for file uploads), novalidate and autocomplete. On fields: name, value, required, placeholder, disabled, readonly, autocomplete, and the validation attributes min, max, minlength, maxlength and pattern.

<form action="/upload" method="post" enctype="multipart/form-data">
  <input name="username" required minlength="3" maxlength="20"
         pattern="[a-z0-9_]+" autocomplete="username">
  <input name="age" type="number" min="13" max="120">
  <input name="avatar" type="file" accept="image/*">
  <button>Save</button>
</form>

Form Attributes Overview

HTML form attributes are powerful tools that control every aspect of form behavior - from where data is sent to how it's encoded, validated, and processed by browsers. Understanding these attributes is crucial for creating secure, user-friendly, and efficient web forms.

Core Form Attributes:

  • action - Data destination URL
  • method - HTTP method (GET/POST)
  • enctype - Data encoding format
  • target - Response destination
  • autocomplete - Browser assistance
  • novalidate - Validation control
  • name - Form identifier
  • accept-charset - Character encoding

Try It Yourself

Explore how different form attributes work with this comprehensive example:

Code Editor

<form id="f" action="/signup" method="post" autocomplete="on">
  <input name="username" placeholder="username" required minlength="3" pattern="[a-z0-9_]+" autocomplete="username">
  <input name="age" type="number" min="13" max="120" placeholder="age">
  <input name="code" value="ABC-123" readonly>
  <input name="ignored" value="not sent" disabled>
  <button>Submit</button>
</form>
<pre id="out"></pre>

Preview

What to Notice:

  • • Different methods (GET vs POST) for different purposes
  • • Multipart encoding enables file uploads
  • • Autocomplete values help users fill forms faster
  • • Target attribute controls where responses appear

action Attribute

The actionattribute specifies the URL where the form data should be sent when submitted.

Relative URLs:

/api/submit

Root relative

../handler.php

Parent directory

process.aspx

Same directory

Absolute URLs:

https://api.example.com/submit

External API

mailto:contact@example.com

Email submission

"" (empty)

Current page

method Attribute

The methodattribute defines how to send form data to the server.

GET Method

Data is sent as URL parameters, visible in the address bar.

example.com?name=John&email=john@email.com

Characteristics:

  • • Data visible in URL
  • • Can be bookmarked
  • • Limited data size (2048 chars)
  • • Cached by browsers
  • • Not secure for sensitive data

Best for:

  • • Search forms
  • • Filtering data
  • • Navigation parameters
  • • Public information requests

POST Method

Data is sent in the request body, not visible in the URL.

Request Body: name=John&email=john@email.com

Characteristics:

  • • Data hidden from URL
  • • No size limitations
  • • More secure
  • • Not cached
  • • Can't be bookmarked

Best for:

  • • Login forms
  • • Registration forms
  • • File uploads
  • • Sensitive data submission

enctype Attribute

The enctypeattribute specifies how form data should be encoded when submitting to the server.

application/x-www-form-urlencoded (default)

Standard encoding for text data. Converts spaces to + and special characters to percent-encoded values.

name=John+Doe&email=john%40example.com

multipart/form-data

Required for file uploads. Each form field is sent as a separate part with its own headers.

File uploads will fail without this encoding type!

text/plain

Rarely used. Sends data as plain text without encoding. Mainly for email forms.

autocomplete Attribute

The autocompleteattribute controls whether browsers should provide autocomplete functionality.

Common Autocomplete Values:

Personal Info:

  • • name - Full name
  • • given-name - First name
  • • family-name - Last name
  • • email - Email address
  • • tel - Phone number
  • • bday - Birthday

Address:

  • • street-address - Address
  • • address-level1 - State/Province
  • • address-level2 - City
  • • postal-code - ZIP code
  • • country - Country

Security:

  • • current-password - Current password
  • • new-password - New password
  • • one-time-code - 2FA code
  • • cc-number - Credit card

Additional Form Attributes

Validation & Behavior:

novalidate:

Disables HTML5 validation

target:

Where to display response (_self, _blank, _parent, _top)

Identification:

name:

Form name for JavaScript access

accept-charset:

Character encoding for form data

Form Attribute Best Practices

Do's:

  • • Use POST for sensitive data
  • • Set proper enctype for file uploads
  • • Implement semantic autocomplete
  • • Always validate server-side
  • • Use HTTPS for sensitive forms
  • • Test with different browsers

Don'ts:

  • • Relying only on client validation
  • • Using GET for sensitive data
  • • Forgetting multipart for files
  • • Disabling autocomplete unnecessarily
  • • Missing CSRF protection
  • • Ignoring accessibility

Security Reminder

Never trust client-side validation alone! HTML5 validation can be easily bypassed. Always implement robust server-side validation and use HTTPS for sensitive forms.

Quick Reference

Essential Attributes:

action: URL destination for form data
method: HTTP method (get, post)
enctype: Data encoding type
autocomplete: Browser assistance control

Example Usage:

<form action="/api/contact" method="post" enctype="multipart/form-data" autocomplete="on"> <!-- Form controls --> </form>

HTML Form Attributes — step by step

1 / 4
1

Use required for mandatory fields

The required attribute prevents form submission if the field is empty. The browser shows a built-in error message and focuses the empty field.

1 of 4
💡HTML5 Validation First
Use HTML5 built-in validation (required, type, pattern, min, max) before writing JavaScript validators. It's simpler, automatically accessible, and works without JavaScript enabled. Add JavaScript validation only for complex rules the browser can't handle.

🎯 Quick Check

Which attribute lets you define a custom validation pattern using a regular expression?

HTML Form Attributes: summary

Key points

  • disabled fields aren't submitted; readonly fields are submitted but can't be edited.
  • The form attribute links a control outside the <form> element to it by ID.
  • formaction and formmethod on a button override the form's settings for that button.

Frequently asked questions

›What is the difference between disabled and readonly?

A readonly field can be focused and its value is submitted. A disabled field can't be focused and isn't submitted.