What Are HTML Form Attributes?
HTML form attributes are special properties that provide additional information about a form or its elements. They define how a form behaves, such as where to send the data, which HTTP method to use, and how to validate input fields.
For example, a form without attributes might look incomplete:
<form>
<input type="text" name="username">
<button type="submit">Submit</button>
</form>
With attributes, the form becomes functional and user-friendly:
<form action="/submit" method="POST">
<input type="text" name="username" required>
<button type="submit">Submit</button>
</form>
Commonly Used HTML Form Attributes
1. action Attribute
The action attribute specifies the URL where form data will be sent for processing. This is essential for ensuring that the submitted data reaches the intended destination.
Example:
<form action="https://example.com/process-form" method="POST">
<input type="text" name="username" required>
<button type="submit">Submit</button>
</form>
- Explanation: When the user submits this form, the data will be sent to https://example.com/process-form.
2. method Attribute
The method attribute specifies the HTTP method used to send the form data. The two most common methods are:
- GET: Appends the form data to the URL. Best for non-sensitive data like search queries.
- POST: Sends data securely in the request body. Best for sensitive information like passwords.
Example (GET Method):
<form action="/search" method="GET">
<input type="text" name="query" placeholder="Search...">
<button type="submit">Search</button>
</form>
Example (POST Method):
<form action="/login" method="POST">
<input type="text" name="username" required>
<input type="password" name="password" required>
<button type="submit">Login</button>
</form>
3. enctype Attribute
The enctype attribute specifies how form data should be encoded before sending it to the server. It’s mainly used when forms include file uploads. Common values are:
- application/x-www-form-urlencoded (default): Used for simple data.
- multipart/form-data: Required for file uploads.
- text/plain: Sends plain text data.
Example:
<form action="/upload" method="POST" enctype="multipart/form-data">
<label for="file">Upload File:</label>
<input type="file" id="file" name="file">
<button type="submit">Upload</button>
</form>
4. target Attribute
The target attribute specifies where to display the response after the form is submitted. Possible values include:
- _self (default): Loads the response in the same tab.
- _blank: Opens the response in a new tab.
- _parent: Loads the response in the parent frame.
- _top: Loads the response in the full body of the window.
Example:
<form action="/submit" target="_blank">
<input type="text" name="name">
<button type="submit">Submit</button>
</form>
- Explanation: The form submission response will open in a new browser tab.
5. novalidate Attribute
The novalidate attribute disables HTML form validation. By default, forms validate user input based on attributes like required and pattern. Use novalidate if you want to handle validation manually.
Example:
<form action="/submit" method="POST" novalidate>
<input type="email" name="email" placeholder="Enter email">
<button type="submit">Submit</button>
</form>
6. autocomplete Attribute
The autocomplete attribute specifies whether the browser should suggest previously entered values for the form fields.
- Values:
- on (default): Enables autocomplete.
- off: Disables autocomplete.
Example:
<form action="/submit" method="POST" autocomplete="off">
<input type="text" name="username">
<input type="password" name="password">
<button type="submit">Submit</button>
</form>
7. name Attribute
The name attribute is crucial for identifying form elements. When the form is submitted, the name attribute is used to send the data in key-value pairs.
Example:
<form action="/submit" method="POST">
<input type="text" name="username">
<input type="email" name="email">
<button type="submit">Submit</button>
</form>
8. id Attribute
The id attribute uniquely identifies a form or its elements. It is often used for styling or linking <label> tags to their respective input fields.
Example:
<form action="/submit" id="contactForm">
<label for="username">Name:</label>
<input type="text" id="username" name="username">
<button type="submit">Submit</button>
</form>
9. accept-charset Attribute
The accept-charset attribute specifies the character encoding used for form submission. Common values are UTF-8 and ISO-8859-1.
Example:
<form action="/submit" accept-charset="UTF-8">
<input type="text" name="name">
<button type="submit">Submit</button>
</form>
10. onsubmit Attribute
The onsubmit attribute specifies JavaScript code to execute when the form is submitted.
Example:
<form action="/submit" onsubmit="return validateForm()">
<input type="text" name="username" id="username">
<button type="submit">Submit</button>
</form>
<script>
function validateForm() {
const username = document.getElementById('username').value;
if (username === '') {
alert('Username is required!');
return false;
}
return true;
}
</script>