How to Disable the Browser Autocomplete and Autofill on HTML Form and Input Fields
When a user starts typing in the HTML form field, browsers, by default, enable the autocomplete feature. Many users let their browsers collect form data allowing using autocomplete in forms in the future. However, there can be situations when this can not work accurately, or you might prefer that users manually insert all the information in details. Moreover, there might be a privacy concern for users, so browsers can let users disable it.
Some data presented in forms, however, are either not important in the future (such as a one-time pin) or include confidential information (such as a specific government identification or security code for bank cards). As the author of a website, you might prefer the browser not to remember the values for that kind of fields, even though the autocomplete feature of the browser is enabled. This allows the browser to offer autocompletion (i.e., display possible completions for the fields where the user has started typing) or autofill (i.e., pre-populate some fields on load).
How to Disable Autocomplete
To disable the autocomplete of text in forms, use the autocomplete attribute of <input> and <form> elements. You'll need the "off" value of this attribute.
This can be done in a <form> for a complete form or for specific <input> elements:
- Add autocomplete="off" onto the <form> element to disable autocomplete for the entire form.
- Add autocomplete="off" for a specific <input> element of the form.
Let’s see an example where the autocomplete is set to "off" for the whole form and two <input> elements.
Example of using the autocomplete attribute with the "off" value:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
input {
margin: 5px 0;
padding: 5px;
}
</style>
</head>
<body>
<form action="/form/submit" method="get" autocomplete="off">
<div>
<input type="text" name="Name" placeholder="First Name" autocomplete="off">
</div>
<div>
<input type="text" id="lname" name="Surname" placeholder="Last Name" autocomplete="off">
</div>
<div>
<input type="number" name="Credit card number" placeholder="Credit card number">
</div>
<input type="submit" value="Submit">
</form>
</body>
</html>
Let's see another example. Here, you'll need the following steps:
- Add autocomplete="off" to the <form> element.
- Add a hidden <input> with autocomplete="false" as a first child element of the form.
Example of disabling the autocomplete:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
input:not[type="submit"] {
background-color: #ffffff;
border: 1px solid #cccccc;
padding: 5px;
}
input:-webkit-autofill {
-webkit-box-shadow: 0 0 0 1000px white inset !important;
}
.hidden {
display: none;
}
</style>
</head>
<body>
<form autocomplete="off" method="post" action="/form/submit">
<input autocomplete="false" name="hidden" type="text" class="hidden">
<div>
<label for="name">Name
<input type="text" name="name" Id="name" placeholder="Enter Your Name">
</label>
</div>
<br/>
<div>
<label for="email">Email
<input type="Email" name="email" Id="email" placeholder="Enter Your Email">
</label>
</div>
<br/>
<input type="submit" value="Submit">
</form>
</body>
</html>
There are two effects of setting autocomplete="off " for your form fields:
- Tells the browser not to save user-input data on similar forms for later autocompletion, although browser-specific algorithms vary.
- Prevents the browser from caching form data in the session history. When the data of forms is stored in the session history, the information filled in by the user will be still displayed, even if the user has submitted the form and clicked the Back button to return to the initial form page.
As many users wouldn’t like the browser to remember passwords, modern browsers do not support the autocomplete="off " for login fields. For login fields, there is no difference whether autocomplete is disabled for <input> fields or <form> fields.
When a website sets autocomplete="off " for a <form> or <input> element, and username/password fields are included in it, the browser will still allow remembering these fields, and if the user accepts, the browser will autofill the fields the next time when the user visits that website.
In that way behave Firefox (since version 38), and Google Chrome (since version 34).
In the case, you represent a user management page where a user can specify a new password for a different user, and you don’t want to autofill password fields, use autocomplete="new-password", which is a sign for browsers that they are not required to react to it.
If Google Chrome remembers a login or password, it will change the background to yellow. To remove the background color, you can try the following.
Example of removing the yellow background from the field:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
input:-webkit-autofill {
-webkit-box-shadow: 0 0 0 1000px white inset !important;
}
</style>
</head>
<body>
<form action="/form/submit" method="GET">
<div>
<label for="name">Name
<input type="text" name="name" Id="name" placeholder="Enter Your Name">
</label>
</div>
<br/>
<div>
<label for="email">Email
<input type="email" name="email" Id="email" placeholder="Enter Your Email">
</label>
</div>
<br/>
<input type="submit" value="Submit">
</form>
</body>
</html>
There are some probably better ways of disabling the autocomplete and autofill with Javascript and jQuery. Let's also see some examples with them before you can decide which is best for your code.
Example of disabling the autocomplete with Javascript:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Title of the Document</title>
</head>
<body>
<form action="/form/submit" method="post">
<div class="form-group">
<input type="text" name="username" placeholder="Enter Name">
<input type="email" name="email" placeholder="Enter Email">
<button type="submit"> Submit </button>
</div>
</form>
<script>
let tagArr = document.getElementsByTagName("input");
for (let i = 0; i < tagArr.length; i++) {
tagArr[i].autocomplete = 'off';
}
</script>
</body>
</html>
Example of disabling the autocomplete using Javascript:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Title of the Document</title>
<script src="https://code.jquery.com/jquery-3.5.0.min.js"></script>
</head>
<body>
<form action="/form/submit" method="post">
<div class="form-group">
<input type="text" id="name" name="username" placeholder="Enter Name">
<input type="email" id="email" name="email" placeholder="Enter Email">
<button type="submit">Submit</button>
</div>
</form>
<script>
$(document).ready(function() {
$('input').attr('autocomplete', 'off');
});
</script>
</body>
</html>
Example of disabling the autocomplete with jQuery:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Title of the Document</title>
<script src="https://code.jquery.com/jquery-3.5.0.min.js">
</script>
</head>
<body>
<form action="/form/submit" method="post">
<div class="form-group">
<input type="text" id="name" name="username" placeholder="Enter Name">
<input type="email" id="email" name="email" placeholder="Enter Email">
<button type="submit">Submit</button>
</div>
</form>
<script>
$(document).ready(function() {
try {
$("input[type='text']").each(function() {
$(this).attr("autocomplete", "off");
});
}
catch (e)
{}
});
</script>
</body>
</html>
Example of disabling the autofill with jQuery:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"
integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="
crossorigin="anonymous"></script>
<script src="https://terrylinooo.github.io/jquery.disableAutoFill/assets/js/jquery.disableAutoFill.min.js"></script>
</head>
<body>
<form action="/form/submit" method="post" id="login-form">
<input name="username" type="text" placeholder="username" />
<input name="password" type="password" placeholder="password" />
<input type="submit" value="Submit">
</form>
<script>
$(function() {
$('#login-form').disableAutoFill();
});
</script>
</body>
</html>