input tags
field
web form
autocomplete
browser

How do you disable browser autocomplete on web form field / input tags?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

To disable browser autocomplete on form fields or <input> tags, you can use the autocomplete attribute with the value off. While most browsers honor this setting, there are a few nuances to consider.


1. Basic Solution: Add autocomplete="off"

You can add the autocomplete="off" attribute to individual form fields or the entire <form> element.

Example 1: Disabling Autocomplete for the Entire Form

html
1<form action="/submit" method="post" autocomplete="off">
2    <label for="username">Username:</label>
3    <input type="text" id="username" name="username">
4
5    <label for="password">Password:</label>
6    <input type="password" id="password" name="password">
7
8    <button type="submit">Submit</button>
9</form>
  • Adding autocomplete="off" to the <form> disables autocomplete for all child input fields.

Example 2: Disabling Autocomplete for Specific Fields

You can disable autocomplete only for specific input fields.

html
1<form action="/submit" method="post">
2    <label for="username">Username:</label>
3    <input type="text" id="username" name="username" autocomplete="off">
4
5    <label for="email">Email:</label>
6    <input type="email" id="email" name="email">
7
8    <button type="submit">Submit</button>
9</form>
  • In this case, autocomplete is disabled only for the username field, but it remains enabled for the email field.

2. For Modern Browsers: Use autocomplete="new-password"

Some modern browsers may ignore autocomplete="off", particularly for password managers. Instead, use autocomplete="new-password" for fields like passwords or sensitive input.

Example: Password Field

html
1<form action="/submit" method="post">
2    <label for="password">Password:</label>
3    <input type="password" id="password" name="password" autocomplete="new-password">
4</form>
  • autocomplete="new-password" tricks browsers into not autofilling the password field, as they think it's asking for a new password.

3. Adding Dummy Input Fields (Workaround)

Some browsers may still auto-fill login forms despite autocomplete="off". A common workaround is to add a dummy hidden input field before the actual input.

Example: Dummy Input

html
1<form action="/submit" method="post" autocomplete="off">
2    <!-- Hidden dummy input field -->
3    <input type="text" name="fake_username" style="display: none;">
4    
5    <!-- Actual input fields -->
6    <label for="username">Username:</label>
7    <input type="text" id="username" name="username" autocomplete="off">
8
9    <label for="password">Password:</label>
10    <input type="password" id="password" name="password" autocomplete="new-password">
11
12    <button type="submit">Submit</button>
13</form>
  • The dummy field prevents autofill because the browser may fill the hidden field instead.

4. JavaScript-Based Solution

You can use JavaScript to clear the autocomplete behavior dynamically.

Example: Set autocomplete via JavaScript

html
1<form id="myForm">
2    <label for="username">Username:</label>
3    <input type="text" id="username" name="username">
4
5    <button type="submit">Submit</button>
6</form>
7
8<script>
9    document.getElementById('myForm').setAttribute('autocomplete', 'off');
10    document.getElementById('username').setAttribute('autocomplete', 'off');
11</script>

Browser Compatibility

  • Modern browsers like Chrome, Firefox, Safari, and Edge support autocomplete="off".
  • Some password managers or browsers may ignore autocomplete="off" for login forms due to user convenience features. Use autocomplete="new-password" for better control.

Summary

ApproachWhen to Use
autocomplete="off"General use; works for most form fields.
autocomplete="new-password"For password fields to prevent autofill.
Dummy hidden fieldsWorkaround for stubborn browsers.
JavaScript setAttribute()When dynamic control of autocomplete is needed.

Best Practice: Use autocomplete="off" for general cases and autocomplete="new-password" for sensitive fields like passwords. If necessary, add dummy fields or use JavaScript for stricter control. 🚀


Course illustration
Course illustration

All Rights Reserved.